R.matlab-package {R.matlab} | R Documentation |
This package provides methods to read and write MAT files. It also makes it possible to communicate (evaluate code, send and retrieve objects etc.) with Matlab v6 or higher running locally or on a remote host.
In brief, this package provides a one-directional interface from R to Matlab, with communication taking place via a TCP/IP connection and with data transferred either through another connection or via the file system. On the Matlab side, the TCP/IP connection is handled by a small Java add-on.
The methods for reading and writing MAT files are stable. The R to Matlab interface, that is the Matlab class, is less prioritized and should be considered a beta version.
For package history, see showHistory(R.matlab)
.
This is a cross-platform package implemented in plain R. This package depends on the R.oo package [1].
In order to read compressed MAT files, the Rcompression package is required. To install that package, please see instructions http://www.omegahat.org/cranRepository.html.
To use the Matlab class or requesting verbose output messages, the R.utils package is loaded when needed (and therefore required in those cases).
The readMat()
and writeMat()
methods do not
require a Matlab installation neither do they depend on the
Matlab
class.
To connect to Matlab, Matlab v6 or higher is required.
It does not work with Matlab v5 or before (because those
versions do not support Java).
For confirmed Matlab versions, see the Matlab
class.
To install this package do
install.packages("R.matlab")
To get the "devel" version, see http://www.braju.com/R/.
To get started, see:
readMat
() and writeMat
() - For reading and writing MAT files (Matlab is not needed).
Matlab
- To start Matlab and communicate with it from R.
A related initiative is RMatlab by Duncan Temple Lang and Omegahat. It provides a bi-directional interface between the R and Matlab languages. For more details, see http://www.omegahat.org/RMatlab/. To call R from Matlab on Windows (only), see MATLAB R-link by Robert Henson available at the Matlab Central File Exchange (http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=5051).
Whenever using this package, please cite [2] as
@TECHREPORT{BengtssonH_2005, author = {Bengtsson, Henrik}, title = {{R.matlab} - Local and remote {M}atlab connectivity in {R}}, institution = {Mathematical Statistics, Centre for Mathematical Sciences, Lund University, Sweden}, year = {2005}, type = {{Preprint in Mathematical Sciences (manuscript in progress)}}, note = {[manuscript in progress]}, url = {http://www.maths.lth.se/help/R/R.matlab/}, }
In general:
For trouble shooting in general, rerun erroneous function with
verbose/debug messages turned on. For readMat()
and writeMat()
see their help. For communication with a Matlab server, use
matlab <- Matlab() setVerbose(matlab, threshold=-2)The lower the threshold is the more information you will see.
Cannot connect to Matlab:
If R fails to connect to Matlab, make sure to try the example in
help(Matlab) first. Make sure that the Matlab server is running
before trying to connect to it from R first. If Matlab is running
but open()
times out, make sure Matlab is listening to the
same port that R is trying to connect to. If that does not help,
try to increase the time-out limit, see help(open.Matlab).
Expected an 'answer' from Matlab, but kept receiving nothing.:
When launching a really long Matlab process by evaluate()
, you
may get the above error message.
Reason: This happens because evaluate()
expect a reply
from Matlab as soon as Matlab is done. The waiting should be "blocked",
i.e. it should wait until it receives something. For unknown reasons,
this is not always happening. The workaround we have implemented is to
try readResult/maxTries
waiting readResult/interval
seconds
inbetween.
Solution:
Increase the total waiting time by setting the above options, e.g.
setOption(matlab, "readResult/interval", 10); # Default is 1 second setOption(matlab, "readResult/maxTries", 30*(60/10)); # ~30 minutes
Here is a list of features that would be useful, but which I have too little time to add myself. Contributions are appreciated.
Matlab$createShortcut()
which
creates a Windows shortcut to start the Matlab server
by double clicking it. It should be possible to create
it in the current directory or to the Desktop.
Maybe it is possible to do this upon installation and
even to a Start -> All Programs -> R menu.
who(matlab)
, clear(matlab)
etc.
Can this be done automatically using "reflection", so
that required arguments are automatically detected?
"$"
and
"$<-"
, e.g. matlab$A
and
matlab$A <- 1234
. Is this wanted?
Maybe the same for functions, e.g. matlab$dice(1000)
.
Is it possible to return multiple return values?
If you consider implement some of the above, make sure it is not already implemented by downloading the latest "devel" version!
Thanks to the following people who contributed with valuable feedback, suggestions, code etc.:
The releases of this package is licensed under LGPL version 2.1 or newer.
The development code of the packages is under a private licence (where applicable) and patches sent to the author fall under the latter license, but will be, if incorporated, released under the "release" license above.
[1] H. Bengtsson, The R.oo package - Object-Oriented Programming with References Using Standard R Code, In Kurt Hornik, Friedrich Leisch and Achim Zeileis, editors, Proceedings of the 3rd International Workshop on Distributed Statistical Computing (DSC 2003), March 20-22, Vienna, Austria. http://www.ci.tuwien.ac.at/Conferences/DSC-2003/Proceedings/
[2] Henrik Bengtsson, R.matlab - Local and remote Matlab connectivity in R, Mathematical Statistics, Centre for Mathematical Sciences, Lund University, Sweden, 2005. (manuscript in progress).
Henrik Bengtsson (http://www.braju.com/R/)