What is the difference between a system call from Matlab (using system()) and a command in a cmd terminal?

56 views (last 30 days)
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 Comments
Guillaume
Guillaume on 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?
Arjan Lampe
Arjan Lampe on 29 Jun 2018
Ok, this has been solved. It had to do with an addition to the path variable. Undoing that solves the problem.

Sign in to comment.

Accepted Answer

Arjan Lampe
Arjan Lampe on 20 Sep 2018
It turned out that when using system() or dos() an extra path is included in the path variable. In my case it is this path: C:\Program Files\Legacy Apps\MATLAB\R2016b\bin\win64
This path included a library called libiomp5md.dll which caused a conflict with the program that I was trying to run. Removing this path from the path variable again solved the issue.
  3 Comments
Arjan Lampe
Arjan Lampe on 21 Sep 2018
If in the Matlab prompt you do (assuming windows)
>> [out, txt] = system('set')
>> fid = fopen('MatlabEnv.txt', 'w')
>> fprintf(fid,'%s',txt)
>> close(fid)
And then go to a cmd prompt and do this:
C:\somewhere> set > cmdEnv.txt
Now you have two text files which contain the environment in which commands are executed. If you compare them, you see the difference between them. There is always a variable called 'Path' which contains search paths for, among many other things, dll files. The format is something like:
Path=<dir1>;<dir2>;<dir3>
for example:
Path=C:\Program Files\bin;C:\Program Files (x86)\Common Files;D:\bla
Now, for Matlab this path contained a directory called C:\Program Files\Legacy Apps\MATLAB\R2016b\bin\win64, in the cmd prompt this was not the case. So I ran the matlab command like this to avoid problems:
>> fid = fopen('myBatchFile.bat','w')
>> fprintf(fid,'%s\n','set path=%path:C:\Program Files\Legacy Apps\MATLAB\R2016b\bin\win64;=%');
>> fprintf(fid,'%s\n','someCommand.exe 0 1 0')
>> fclose(fid)
>> system('myBatchFile.bat')
This solved the problem in my case.

Sign in to comment.

More Answers (3)

Swati Lodha
Swati Lodha on 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.

kaiming guo
kaiming guo on 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.

Philipp Krauter
Philipp Krauter on 30 Dec 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');

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Products


Release

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!