What is the difference between a system call from Matlab (using system()) and a command in a cmd terminal?
조회 수: 55 (최근 30일)
이전 댓글 표시
Hi,
I've got Matlab2016b installed on a Windows 7 Professional 64 bits machine.
I've got a specific program that behaves differently when calling it in Matlab using the system() command (or the dos() command, effect is the same) and running the same command from a cmd terminal of Windows. I make sure that the current path of Matlab is the same as the current path of the cmd when I do this function call. The command includes several command line options and looks like this:
In Matlab:
>> disp(pwd)
D:\location
>> system('"C:\somewhere\prog.exe" 0 1 0')
ans =
-1.073741512000000e+09
In the cmd terminal:
D:\location> "C:\somewhere\prog.exe" 0 1 0
<... lots of output ...>
D:\location> echo %errorlevel%
0
The %errorlevel% contains the exit code, which is also the return value of the system() call in Matlab. The behavior in the cmd terminal is correct, it generates the results I need.
So what could be going on here? This is not general behavior, but for this specific program. Of course I'm also in contact with the developers of that program, but I would like to know what the difference is between these two ways of running a program.
댓글 수: 3
Guillaume
2018년 6월 29일
Arjan Lampe's comment mistakenly posted as an answer moved here (please use comments unless answering the question):
Is it? -1.073741512000000e+09 does not seem to be 0.
That's exactly the problem :) I've no clue why this would be the return value.
What do you get if you catch the outputs?
out is empty
Does the environment contain different settings?
Ok, that's a good point. There is indeed a difference in the environment. And when I make them equal, I get the same error in the cmd.
It has to do with this library: libiomp5md.dll
In the matlab environment this exists:
__KMP_REGISTERED_LIB_6028=000007FEE9DE7954-cafed93d-libiomp5md.dll
Can this be undone? Or why is it there in the first place?
채택된 답변
추가 답변 (3개)
Swati Lodha
2018년 7월 2일
A system call from using MATLAB using system():
>> status = system(command)
calls the operating system to execute the specified command. The operation waits for the command to finish execution before returning the exit status of the command to the status variable. The function starts a new cmd/shell process, executes command, exits the process, and returns to the MATLAB® process. Updates to the system environment made by command are not visible to MATLAB.
Whereas, The operating system command is specified as a string or a character vector like "dir", "ls" etc. The command executes in a system shell, which might not be the shell from which you started MATLAB.
댓글 수: 0
kaiming guo
2018년 9월 20일
I have exactly same issue. Is it due to environment setting? I wish to get some hints on how to set up the environment for this issue.
댓글 수: 2
kaiming guo
2018년 9월 21일
Thanks for your answers. Really appreciated! Hopefully, you have seen my comment. I may need more descriptions.
Philipp Krauter
2018년 12월 30일
There is a simple workaround for the problem. Newer versions of Matlab are adding a folder to the Path environmental variable each time, system() or dos() function are called. For me, removing this new path by calling
system('set path=%path:C:\Program Files\MATLAB\R2018b\bin\win64;=% & myprog.exe');
instead of
system('myprog.exe');
solves the problem.
Please note, that the folder to be removed from the environmental variable can differ. It can be read out using
system('path');
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!