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

68 views (last 30 days)
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)
>> system('"C:\somewhere\prog.exe" 0 1 0')
ans =
In the cmd terminal:
D:\location> "C:\somewhere\prog.exe" 0 1 0
<... lots of output ...>
D:\location> echo %errorlevel%
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.
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.

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
solves the problem.
Please note, that the folder to be removed from the environmental variable can differ. It can be read out using





Community Treasure Hunt

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

Start Hunting!