Matlab on macOS .matlab7rc.sh shell config

On macOS, when started from the Applications icon, Matlab does NOT source shell configuration files. Any environment variables set in these files such as those added by package managers like Homebrew or Conda will not be available in the Matlab environment.

On macOS, only if Matlab is started from the Terminal like /Applications/MatlabR20*.app/bin/matlab will Matlab source the shell configuration file, leading to the expected environment variables as default in an interactive login shell also available in Matlab.

Instead of depending on how the user launched Matlab, a more uniform approach is to have users set environment variables in the Matlab startup.m script, which will be sourced regardless of how Matlab is launched. Set desired environment variables in the Matlab startup.m script like:

edit(fullfile(userpath,'startup.m'))

Add lines like

setenv('PATH', ['/opt/homebrew/bin/', pathsep, getenv('PATH')])

Where ‘/opt/homebrew/bin’ was obtained from the system command brew --prefix. We recommend not running system() scripts in startup.m unless truly necessary to avoid delaying Matlab startup time or affecting startup stability.

Then programs installed by Homebrew like CMake, GCC, etc. will be on Path environment variable in Matlab.

Note that the Matlab commands below only affect commands within that same command line:

!source ~/.zshrc && ls
system("source ~/.zshrc && ls")