How to Run a R script from Matlab [duplicate]

匿名 (未验证) 提交于 2019-12-03 08:28:06

问题:

This question already has an answer here:

I have .m file, using which I want to run a R script. How can I do this.

Matlab File

caller.m  %some matlab code  % need to call a R script  %some matlab code 

R script

script.R   some R code 

I have both the files in the same folder.

How to run script.R from caller.m?


Answer by Drew Steen is in general true, as I found this on most of the places on web. But what worked for me, I am describing below:

Steps:

  1. Append "C:\Program Files\R\R-2.15.3\bin\x64" to "path" variable . This link provides procedure to set path in windows 7 os. Note that bin\x64 instead of bin, bin didn't worked for me.

  2. Restart Matlab.

  3. Use exec=system('Rscript.exe script.R') where the current script.R is in the current directory of matlab.

回答1:

You can use the system function in MATLAB to execute shell commands. Since you can run R from batch files,

executed = system('R CMD BATCH path/script.R') 

should work. Note that path will need to be the correct relative path to your R script from whatever your active directory in MATLAB is. executed will evaluate as 0 if the system command executed successfully (which is not the same as the R script executing successfully).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!