matlabr: a Package to Calling MATLAB from R with system
2015-04-09 12:40
387 查看
(This article was first published on A HopStat and Jump Away » Rbloggers, and kindly contributed to
R-bloggers)
In my research, I primarily use
RMatlab package (which can call
Previously,
Mandy Mejia wrote “THREE WAYS TO USE MATLAB FROM R”. Option 2 is about how to use
I like this strategy options because:
I didn’t take the time to learn
It worked for me.
I wrote a package to wrap the options Mandy described:
try-catch statement in MATLAB so that if it fails, it will print the error message. Without this try-catch, if MATLAB errors, then running the command will remain in MATLAB and not return to
Setting up
Let’s set up the
The result from
R-bloggers)
In my research, I primarily use
R, but I try to use existing code if available. In neuroimaging and other areas, that means calling MATLAB code. There are some existing solutions for the problem of
Rto MATLAB: namely the
R.matlabpackage and the
RMatlab package (which can call
Rfrom MATLAB as well). I do not use thse solutions usually though.
Previously,
Mandy Mejia wrote “THREE WAYS TO USE MATLAB FROM R”. Option 2 is about how to use
R.matlab, and Mandy gives and example with some cod. She also describes in Options 1 and 3 how to use the
systemcommand to call MATLAB commands.
I like this strategy options because:
I didn’t take the time to learn
R.matlab.
It worked for me.
I wrote a package to wrap the options Mandy described:
matlabr.
matlabr: Wrapping together system calls to MATLAB
Thematlabrpackage is located in GitHub and you can install it with the following command:
get_matlab: Mostly internal command that will return a character string that will be passed to
system. If
matlabis in your PATH (bash variable), and you are using R based on the terminal, the command would return
"matlab". If MATLAB is not in your PATH or using a GUI-based system like RStudio, you must set
options(matlab.path='/your/path/to/matlab').
have_matlab: Wrapper for
get_matlabto return a logical if
matlabis found.
run_matlab_script: This will pass a
.mfile to MATLAB. It also wraps the command in a
try-catch statement in MATLAB so that if it fails, it will print the error message. Without this try-catch, if MATLAB errors, then running the command will remain in MATLAB and not return to
R.
run_matlab_code: This takes a character vector of MATLAB code, ends lines with
;, writes it to a temporary
.mfile, and then runs
run_matlab_scripton the temporary
.mfile.
rvec_to_matlab: Takes in a numeric
Rvector and creates a MATLAB column matrix.
rvec_to_matlabclist: Takes in a vector from
R(usually a character vector) and quotes these strings with single quotes and places them in a MATLAB cell using curly braces:
{and
}. It then stacks these cells into a “matrix” of cells.
Setting up matlabr
Let’s set up the matlab.pathas I’m running in RStudio:
library(matlabr) options(matlab.path = "/Applications/MATLAB_R2014b.app/bin") have_matlab()
The result from
have_matlab()indicates that the
matlabcommand can be called.
Let’s write some code to test it
Here we will create some code to take a value forx,
y,
z(scalars) and a matrix named
aand then save
x,
a,
zto a text file:
code = c("x = 10", "y=20]/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpHnOinq/file2f8352c04937.mOutput
First off, we see thattest.txtindeed was written to disk.file.exists("test.txt") [/code][1] TRUE
We can read in thetest.txtfrom usingreadLines:output = readLines(con = "test.txt") print(output)[1] " 1.0000000e+01" [2] " 1.0000000e+00 2.0000000e+00 3.0000000e+00" [3] " 4.0000000e+00 5.0000000e+00 6.0000000e+00" [4] " 7.0000000e+00 8.0000000e+00 1.0000000e+01" [5] " 3.0000000e+01"Conclusions
matlabrisn’t fancy and most likely has some drawbacks as usingsystemcan have some quirks. However, these functions have been helpful for me to use some
SPM routines and other MATLAB commands while remaining “withinR“.R.matlabhas a better framework, but it may not be as straightforward for batch processing. Alsomatlabrhas some wrappers that will do a try-catch so that you don’t get stuck in MATLAB after callingsystem.
Let me know if this was helpful or if you have ideas on how to make this better. Or better yet, give a
pull request.
相关文章推荐
- matlabr: a Package to Calling MATLAB from R with system
- 转SSIS 2012 pass values from child package to parent with project deployment model
- PDFToText with ITextSharp--Extract text from PDF in C# (100% .NET)(推荐)
- AjaxHelper v0.56 (Updated to Version 0.56 from 0.55 with little changes)
- Matlab排错:conversion to double from sym is impossile
- How to use CreateChildContorls method inherited from System.Web.UI.Control
- Migrating from VS 2005 to VS 2008 with Microsoft.VisualStudio.Shell references (译)
- How to debug code from rt.jar with JDK 1.4.1?
- Beginning Google Maps Applications with Rails and Ajax From Novice to Professional - Chapter One
- Migrating from VS 2005 to VS 2008 with Microsoft.VisualStudio.Shell references (译)
- Beginning XML with DOM and Ajax: From Novice to Professional
- What 's CATT(Computer Aided Test Tool)?How to deal with the CATT in the SAP System?什么是CATT(计算机附加测试工具)
- System error &H80004015 - This class is configured to run as a security id different from the caller.
- How do I set up a Microsoft Visual Studio project to work with MATLAB Compiler 4.0?
- Ten things to do with IIS(From CodeProject website)
- Calling Pro*C program from a package", version 8.0.5
- Confige to auto-update from loacl with new Kaspersky Virus Library
- Ten things to do with IIS(From CodeProject website)
- Beginning JavaScript with DOM Scripting and Ajax: From Novice to Professional
- Darren Jefford : Calling a .NET Assembly from Orchestration with Schema Types