如何从Matlab运行R脚本

如何从Matlab运行R脚本

本文介绍了如何从Matlab运行R脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有.m文件,我想使用该文件运行R脚本.我该怎么做.

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

Matlab文件

caller.m

%some matlab code

% need to call a R script

%some matlab code

R脚本

script.R

some R code

两个文件都放在同一个文件夹中.

I have both the files in the same folder.

如何从caller.m运行script.R?

How to run script.R from caller.m?

Drew Steen的答案通常是正确的,因为我在网络上的大多数地方都发现了这一点.但是对我有用的是,我在下面描述:

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:

步骤:

  1. 将"C:\ Program Files \ R \ R-2.15.3 \ bin \ x64"追加到"path"变量.此 link 提供了在Windows 7 OS中设置路径的过程.请注意,bin \ x64代替bin,bin不适用于我.

  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.

重新启动Matlab.

Restart Matlab.

使用exec = system('Rscript.exe script.R'),其中当前脚本.R在matlab的当前目录中.

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

推荐答案

您可以在MATLAB中使用system函数来执行Shell命令.由于您可以从批处理文件运行R,因此

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')

应该工作.请注意,无论您在MATLAB中的活动目录如何,path都必须是R脚本的正确相对路径.如果系统命令成功执行(与R脚本成功执行不同,则executed的计算结果将为0.)

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).

这篇关于如何从Matlab运行R脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 04:07