What is the difference between a system call from Matlab (using system()) and a command in a cmd terminal?
59 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Arjan Lampe
am 29 Jun. 2018
Beantwortet: Philipp Krauter
am 30 Dez. 2018
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 Kommentare
Guillaume
am 29 Jun. 2018
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?
Akzeptierte Antwort
Arjan Lampe
am 20 Sep. 2018
3 Kommentare
kaiming guo
am 21 Sep. 2018
Thanks a lot! Now it is working again. Appreciate your time and patient!
Weitere Antworten (3)
Swati Lodha
am 2 Jul. 2018
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 Kommentare
kaiming guo
am 20 Sep. 2018
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 Kommentare
kaiming guo
am 21 Sep. 2018
Thanks for your answers. Really appreciated! Hopefully, you have seen my comment. I may need more descriptions.
Philipp Krauter
am 30 Dez. 2018
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 Kommentare
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!