2016-05-13 25 views
0

执行批处理文件,我有一个Matlab函数,发现其中这个功能我的电脑内的路径,然后运行在同一个目录下一个bat文件。这个bat文件是为了执行一个R脚本,但是由于一个奇怪的原因而没有这么做。从MATLAB

这是我的Matlab的功能:

function [] = myFunction(arg) 

    % Find the directory of the executing script 
    thisDir = fileparts(mfilename('fullpath')); 

    % Save arg as a csv on this directory, this will be read by my R script 
    tmpDir = strcat(thisDir,'/tmp.csv'); 
    csvwrite(tmpDir,arg); 

    % Specify the command to run 
    dosCommand = ['call "' thisDir '/runRscript.bat"']; 
    dos(dosCommand); 

end 

BAT文件具有下面的代码:

"C:\Program Files\R\R-3.2.2\bin\x64\R.exe" CMD BATCH runRScipt.R 

当我在Matlab运行功能,我得到了以下信息:

C:\用户\ ... mypath中...> “C:\ Program Files文件\ r \ R-3.2.2 \ BIN \ 64 \运行R.exe” CMD批 runRscript.R

由于我在Matlab中得到这个消息,我毫不怀疑它正在查找和读取批处理文件,但是它无法执行R脚本。我知道bat文件按预期工作,因为我可以通过命令行运行(与应在MATLAB脚本的“dosCommand”命令),或者通过在.bat文件点击两次。

+1

[This](http://stackoverflow.com/questions/14167178/passing-command-line-arguments-to-r-cmd-batch)可能是相关的。 也有似乎是一个特定的包吧,见[这里](http://www.mathworks.com/matlabcentral/answers/31708-running-r-within-matlab)。 –

回答

0

我找到了答案。出于一个奇怪的原因,dos()命令不起作用,但system()命令将完成这项工作。然后代码将看起来像这样:

function [] = myFunction(arg) 

    % Find the directory of the executing script 
    thisDir = fileparts(mfilename('fullpath')); 

    % Save arg as a csv on this directory, this will be read by my R script 
    tmpDir = strcat(thisDir,'/tmp.csv'); 
    csvwrite(tmpDir,arg); 

    % Specify the command to run 
    sysCommand = ['call "' thisDir '/runRscript.bat"']; 
    system(sysCommand); 

end 

而且批处理文件:

“C:\ Program Files文件\ r \ R-3.2.2 \ BIN \ 64 \运行R.exe” CMD BATCH runRScipt.R

0

而是运行R.exe的请尝试Rscript.exe。 R.exe在交互式mdoe中运行R代码,而Rscript以批处理模式运行代码。理想情况下,你应该找到RSCRIPT可执行文件在相同的路径中的R可执行文件(即 “C:\ Program Files文件\ r \ R-3.2.2 \ BIN \ 64” 你的情况)

+0

感谢您的回答@abhiieor。 Rscript.exe是在相同的路径,但即使我改变它,我仍然不能从Matlab执行它。此外,通过此更改,批处理文件不再通过双击执行R脚本。 – Victor