Why won't R.matlab connect to MATLAB?

Viewed 18

I am trying to use R.matlab for the first time. I run the following my_sample_program.R in MATLAB using the command:

system('/usr/local/bin/R CMD BATCH my_sample_program.R out.txt');

where the R code is as follows:

test<-function(x,y)
{
  library(R.matlab)
  matlab <- Matlab(host="localhost",port=9927)
  open(matlab)
  setVerbose(matlab,-2)
  x <- getVariable(matlab, "x")
  y <- getVariable(matlab, "y")
  some_other_function(x,y,exact = TRUE)
}

test(x,y)

Running the system command is successful from MATLAB's point of view. But I get the error in the out.txt file that says

Error in throw.default(sprintf("Failed to connect to MATLAB on host '%s' (port %d) after trying %d times for approximately %.1f seconds.",  : 
  Failed to connect to MATLAB on host 'localhost' (port 9927) after trying 30 times for approximately 30.0 seconds.

(I used the command sudo lsof | grep localhost in Terminal to find that MATLAB is associated with port 9927.)

My question: How can I connect to MATLAB via R.matlab?

0 Answers
Related