【问题标题】:Execute batch file from Matlab从 Matlab 执行批处理文件
【发布时间】:2016-09-09 02:54:26
【问题描述】:

我有一个 Matlab 函数,它可以找到这个函数在我的电脑中的路径,然后在同一目录上运行一个 bat 文件。此 bat 文件旨在执行 R 脚本,但由于奇怪的原因未能执行。

这是我的 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:\Users\...mypath...>"C:\Program Files\R\R-3.2.2\bin\x64\R.exe" CMD BATCH 运行Rscript.R

自从我在 Matlab 中收到此消息以来,我毫不怀疑它正在查找和读取批处理文件,但它无法执行 R 脚本。我知道 bat 文件按预期工作,因为我可以通过命令行(使用 Matlab 脚本上的“dosCommand”命令)或在 .bat 文件上单击两次来运行它。

【问题讨论】:

  • This 可能是相关的。似乎还有一个特定的包,请参阅here

标签: r windows matlab batch-file cmd


【解决方案1】:

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

【讨论】:

  • 感谢您的回答@abhiieor。 Rscript.exe 在同一路径上,但即使我更改它,我仍然无法从 Matlab 执行它。此外,通过此更改,批处理文件不再通过双击执行 R 脚本。
【解决方案2】:

我找到了答案。出于一个奇怪的原因,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\x64\R.exe" CMD BATCH runRScipt.R

【讨论】:

    猜你喜欢
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    相关资源
    最近更新 更多