2

I am attempting to execute an external program from Matlab:

cmdstr = sprintf('"%s\\myEXECUTABLE" "%s" -options',fullEXEpath, fullInputFilePath);
[status, res] = system(cmdstr);

I receive "status = 1", partial program output in "res" (though no error message) and no output files.

BUT, executing exactly the same command with & (ampersand):

cmdstr = sprintf('"%s\\myEXECUTABLE" "%s" -options &',fullEXEpath, fullInputFilePath);
[status, res] = system(cmdstr);

Meaning in the background via a dos command window, works just fine (status equals 0 and output files are created).

I have seen somewhere it might be that the antivirus is blocking the program from executing via Matlab, but I cannot disable it since I am an endpoint user.

Using "&" causes my GUI to open a command window and run in the background, while immediately resuming Matlab code.

I can live with the command window opening, but not with Matlab resuming right away, as I use the output files in my MATLAB code, which are not necessarily ready.

Is there a way to verify the external program has ended? I tried simply:

while (status)  %waiting for system to return status = 0
    disp 'waiting...';
end

but it seems to still return with "status = 0" before completion...

Or rather - is there a way to avoid the &?

Any answer will be much appreciated.

4

1 回答 1

1

作为调试方法,可以使用 system(cmdstr,'-echo')

由于错误消息无处可去,因此您将看不到它们。(matlab 只返回输出,可能不包含错误流)

我是否正确,您正在使用该命令调用 GUI 程序?根据 matlab 文档:

& 字符具有特殊含义。对于控制台程序,这会导致控制台打开。省略此字符会导致控制台程序以图标方式运行。对于 GUI 程序,附加此字符会导致应用程序在后台运行。MATLAB 继续处理。

因此控制台程序(无头程序)不允许 Matlab 在执行时继续。

于 2012-11-09T19:05:26.243 回答