Hassan Saif
Hassan Saif

Reputation: 1072

How to run Matlab from the command line?

Is it possible to run Matlab commands from Mac command line?

Upvotes: 12

Views: 23353

Answers (3)

Böhmeli
Böhmeli

Reputation: 3

As already mentioned above you need to first edit your .bash_profile file by adding the following line (replace 'MATLAB_R2020b' with your MATLAB version)

export PATH=/Applications/MATLAB_R2020b.app/bin:$PATH

Then after restarting the terminal you can open MATLAB by inserting the command

/Applications/MATLAB_R2020b.app/bin/matlab

You can also run your .m scripts by defining the working directory folder and the executable scripts and their paths. You just need to add more commands to the above.

/Applications/MATLAB_R2020b.app/bin/matlab -r "addpath(genpath('{Your working directory folder path}')); cd {Your working directory folder path}; {Your script name}; {Your other script name}; quit;"

Please find a more detailed description of MATLAB command line arguments from:

https://www.mathworks.com/help/matlab/ref/matlabmacos.html#d122e801165

Upvotes: 0

kenm
kenm

Reputation: 23985

The matlab script is in the bin subdirectory of the MATLAB app bundle. On my machine, this means I can run it like so:

/Applications/MATLAB_R2012a_Student.app/bin/matlab

If you want this bin directory on your path (so that you can just run matlab, mex, etc), edit or create a new text file called .bash_profile in the top level of your home directory with the following line:

export PATH=/Applications/MATLAB_R2012a_Student.app/bin:$PATH

Replacing the "MATLAB_R2012a_Student" part with the name of your actual MATLAB app bundle. This will not come into effect for currently open terminals, but newly opened terminals should work properly.

Upvotes: 16

Edric
Edric

Reputation: 25160

You need the full path to the MATLAB executable, and you can use the -r option to run a command in the MATLAB that you start, as per the doc here.

Upvotes: 1

Related Questions