Why does executing Unix commands from MATLAB cause errors on Mac OS?

6 Ansichten (letzte 30 Tage)
I am using MacOS. From the MATLAB command window, if I execute the following command
system('ls')
 
I get the error
/bin/bash: ls: command not found
But the command "ls" works properly from the Mac terminal. Similarly none of the typical OS commands like
system( who), system( which)
work from the MATLAB Command Window but they work from the terminal. Why can't I call Unix commands from MATLAB on Mac OS?

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 14 Dez. 2022
Bearbeitet: MathWorks Support Team am 22 Dez. 2022
If you are starting MATLAB from clicking on the MATLAB icon, then the issue might be caused by this, because when you click on the icon, you are not running MATLAB at the terminal: MATLAB is run by launcher, which does not go through a shell login session. Since MATLAB does not source the "bashrc" file, the PATH set in there is not available to MATLAB if started from the launcher. To verify if this is the cause of the issue, you can launch MATLAB from the terminal and then execute the following commands from the MATLAB Command Window to check if the commands can now be run properly:
system('who')
system('which')
system('ls')
If these commands can now be executed properly from the MATLAB Command Window, then to use the system commands from MATLAB, even if you start from clicking the icon you can set the value of the PATH to point to the proper location by executing the following from the MATLAB Command Window:
 
setenv('PATH', [getenv('PATH') ':/usr/local/bin:/usr/bin:/bin']);
Note that this will set the PATH only for the current session. If after this you can call the system commands properly, then you can add the above command in the "startup.m" file such that every time MATLAB is launched, the command gets executed automatically. Here are the relevant documentation links for your reference:
  1 Kommentar
Walter Roberson
Walter Roberson am 27 Okt. 2019
There are also ways to configure launchctl to put those values into the environment variables when the icon is clicked. The risk with that is that if you move the code to a different system then it might suddenly stop working because the new system does not have the same configuration change.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by