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

6 次查看(过去 30 天)
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?

采纳的回答

MathWorks Support Team
编辑:MathWorks Support Team 2022-12-22
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 个评论
Walter Roberson
Walter Roberson 2019-10-27
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by