Home > other >  how do I mimic the terminal path when making a system call in MATLAB on a mac
how do I mimic the terminal path when making a system call in MATLAB on a mac

Time:11-27

I'm trying to automate mac terminal calls in MATLAB. In my specific use case I used brew to install cmake but in MATLAB cmake isn't recognized [~,result] = system('cmake ..'); returns zsh:1: command not found: cmake

Using the following I am pretty sure I could update the path so that cmake is recognized. (Mac,Matlab,bash) Changing the PATH of bash in Matlab for system commands

However, I was wondering if there was a generic way of mimicking the path that the terminal is seeing.

In particular when I run env in the terminal and in MATLAB using [~,result] = system('env'); the path variables are different and I'm wondering why that is and how to ensure they align.

CodePudding user response:

When you start MATLAB from its icon on macOS, you never ran the login shell startup files, so most of your configuration is not loaded. That is, anything configured in ~/.zprofile, ~/.zshrc, etc. is not seen by MATLAB. Unlike other Unixes, macOS doesn’t start a login shell when you log on. See here for the differences between a login shell and a non-login shell.

One way around this is to launch MATLAB from a terminal window. Another way is to manually load the zsh configuration before running your shell command.

A cleaner solution is to avoid ~/.zprofile (loaded for login shells) and ~/.zshrc (loaded for interactive shells), and instead put your configuration in ~/.zshenv (loaded for all shells, including the one started by MATLAB for system() or !).

In particular, Homebrew adds a line

eval "$(/opt/homebrew/bin/brew shellenv)"

to your ~/.zprofile file. Moving this line to ~/.zshenv and restarting MATLAB should add the Homebrew configuration to shells started from within MATLAB.

  • Related