Effacer les filtres
Effacer les filtres

Executing R code from MATLAB

19 vues (au cours des 30 derniers jours)
L'O.G.
L'O.G. le 10 Fév 2022
This is probably more of an R question than a MATLAB one, but in any case, I am hoping someone can help me. I am trying to use R.matlab for the first time. In MATLAB, I use the command:
system('/usr/local/bin/R CMD BATCH my_sample_program.R out.txt');
Running the command is successful from MATLAB's point of view, so I don't believe this is a MATLAB problem, but maybe I'm wrong. The program my_sample_program.R is in the same directory as the files I wish to analyze, which is also the Current Folder in MATLAB. I find which port MATLAB is using by typing in Terminal:
sudo lsof | grep localhost
Unfortunately, the following commands in R are not able to connect to MATLAB:
library(R.matlab)
matlab <- Matlab(host="localhost",port=9927)
open(matlab)
So, I think I am able to run the command in MATLAB, but R.matlab can't connect to MATLAB. Can somebody please help me? Or let me know of a workaround?

Réponses (1)

Walter Roberson
Walter Roberson le 10 Fév 2022
To confirm:
Inside MATLAB, you are using system() to run R.
Inside R (that is running inside MATLAB), you are using commands that are intended to start a new MATLAB session that is independent of the one that was used to system() out to R ??
But if so, then why are you using lsof to find a port? I get the impression that you are expecting R to be able to connect to the original MATLAB session by using an existing port that MATLAB happens to be listening on (for something) ? But you do not mention anything like having started MATLAB originally with a named pipe and a ptty that might be able to feed commands into it through standard input, so I have no idea what you think MATLAB might be listening on. Unless inside MATLAB you had already used a command to open a socket ???
I wonder if you should be using Matlab$startServer ?
I do not have any experience with R, but I have unix experienc. I can tell you are using some kind of Linux. The MATLAB graphical interface does listen on various sockets to talk between its component parts (for example the help browser is a different process), but it does not listen for external commands. The MATLAB command line (without the graphic interface) does not listen for external commands, but can be fed stdin if you open the command line directly without the graphic interfac.

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Tags

Produits


Version

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by