Help @ MC
The [R] environment
Help @ MC Logo
Centre for Mathematical Sciences, Lund University
    Print this page!
Home

Up to Help

Docs
R Help

Packages
CRAN
R.oo
aroma

Install
The R software
Most packages

Tips'n'Tricks
install.packages
Rgui (Windows)

External Links
R homepage
Download R
bioconductor.org
braju.com packages



Printer-friendly
Print this page



Powered by Google

R.matlab - Local and remote Matlab connectivity in R

- Communicate with Matlab from within R!

Henrik Bengtsson, 2002-2004.

Important: Do only use the code accessable from this page to get an idea what is going under the hood. The latest and most up-to-date version can be found in the R.matlab package, see http://www.braju.com/R/.

Moreover, the R.matlab package provides also readMat() and writeMat() for reading and writing Matlab MAT-files. These two methods are self-contained and do not require Matlab. See package help for more information.

An example how to run a Matlab server access from R

Matlab Server

Start Matlab with Java language support, i.e. version 6 or higher, by:

  % matlab -nodesktop -nosplash
Then start the server inside Matlab by:
  >> MatlabServer

[R] client

In [R]:
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# Start the matlab server on the same machine or on another machine. This
# code will *not* halt and wait for Matlab to get started so you make sure
# you wait long enough for the server to get up and running before you
# make the client try to connect (the client will by default try once a
# second for 30 seconds before giving up).
#
# Internally this code basically calls (in the background)
#
#   matlab -nodesktop -nosplash -r MatlabServer 
#
# The Windows version of Matlab does not seem to support the /r flag (one
# person reported a problem with this);
#
#   matlab /nodesktop /nosplash /r MatlabServer 
#
# So it might be that you have to start matlab and the the MatlabServer
# code "by hand":
#
#   matlab /nodesktop /nosplash 
#   >> MatlabServer 
#
# For this to work, the MatlabServer.m file and the 
# InputStreamByteWrapper.class file (and InputStreamByteWrapper.java ?) 
# has to be in the current working directory of Matlab. You will find
# these files under $R_HOME (or $R_LIBS) under library/R.matlab/data/ 
# (or type print(R.matlab$dataPath) in R to get the path).
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# options(matlab="matlab6.5")
Matlab$startServer()


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# Example of the R standard output when starting Matlab from R prompt:
#
# > Matlab$startServer()
# > Warning: No window system found.  Java option 'nodesktop' ignored.
#  
#  				 < M A T L A B >
#  		     Copyright 1984-2002 The MathWorks, Inc.
#  			 Version 6.5.0.180913a Release 13
#  				   Jun 18 2002
#  
#  
#    To get started, type one of these: helpwin, helpdesk, or demo.
#    For product information, visit www.mathworks.com.
#  
#  ----------------------
#  Matlab server started!
#  ----------------------
#  Server socket created.
# >
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# Start the matlab client on this machine and connect to the specified
# host (any where on the web so watch out if you are not behind a 
# firewall!). If no host is specified, "localhost" is assumed.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# Create a Matlab client
matlab <- Matlab(host="localhost")

# Connect to the Matlab server
if (!open(matlab))
  throw("Matlab server is not running: waited 30 seconds.")

# Run Matlab expressions on the Matlab server
res <- evaluate(matlab, "A=1+2;", "B=ones(2,20);")

# Get Matlab variables
data <- getVariable(matlab, c("A", "B"))
cat("Recieved variables:\n")
str(data)

# Set variables in Matlab
ABCD <- matrix(rnorm(10000), ncol=100)
str(ABCD)
setVariable(matlab, ABCD=ABCD)

# Retrieve what we just set
data <- getVariable(matlab, "ABCD")
cat("Recieved variables:\n")
str(data)

# Create a function (M-file) on the Matlab server
setFunction(matlab, "          \
  function [win,aver]=dice(B)  \
  %Play the dice game B times  \
  gains=[-1,2,-3,4,-5,6];      \
  plays=unidrnd(6,B,1);        \
  win=sum(gains(plays));       \
  aver=win/B;                  \
");

evaluate(matlab, "[w,a]=dice(1000);")
res <- getVariable(matlab, c("w", "a"))
print(res)

# When done, close the Matlab client, which will also shutdown
# the Matlab server and the connection to it.
close(matlab)

Installation

To install this package, please see instructions at http://www.braju.com/R/.

Quick view of the (first version) code

Matlab server code "under the hood" (requires Matlab v6 or later): MatlabServer.m and the required InputStreamByteWrapper.java, which should be compile first.

R client code "under the hood": Matlab.Rex, the class Matlab.R and the utility Java methods Java.R, and the MAT file format reader and writer functions readMAT.R, writeMAT.R.