How to call MATLAB script from command line?

I am trying to call matlab from Terminal (I have a Mac) and it just runs the program for me, without displaying or anything else, just the program. This program displays graphs and writes a text file for me. I found a few previous answers to this question, and so I tried:

matlab -nodisplay -r foo matlab -nodisplay -r foo.m matlab -nojvm -nosplash -nodisplay -r foo;quit; matlab -nojvm -nosplash -nodisplay -r "foo;quit;" matlab -nojvm -nodesktop -r "foo;quit;" matlab -nojvm -nodesktop -r "foo" matlab -nojvm -nodesktop -nosplash -r foo 

Almost every time I try to do this, I still get the same answer (when the line just does not prompt you to say β€œscrew” and give up): the MATLAB program window opens and it remains open. It will interact normally. And the command line won't do anything until I close the window. The program I want does not start. The window is just ... sitting there.

If this helps, here is how I encoded matlab in my inbox:

 #!/bin/bash /Applications/MATLAB_R2015b.app/bin/matlab 

I have no idea what is going on. Any help would be greatly appreciated.

+7
bash matlab
source share
4 answers

Your bash script to call Matlab will not pass any arguments to the Matlab executable. When entering

 $ matlab -nodesktop -nosplash -r "foo" 

what is actually called

 $ /Applications/MATLAB_R2015b.app/bin/matlab 

no arguments. There are several ways to fix this, while retaining the simplicity of calling matlab . Alternatively, you can call the full path to matlab , for example

 $ /Applications/MATLAB_R2015b.app/bin/matlab -nodesktop -nosplash -r "foo" 

Configure matlab executable

Bash script

Given that you have already written a bash script to call matlab , the easiest solution is to change it to include the $@ bash wildcard, for example

 #!/bin/bash /Applications/MATLAB_R2015b.app/bin/matlab " $@ " 

The $@ wildcard passes all the parameters that you use, for example, -nodesktop -nosplash -r "foo" to the matlab executable, so now it’s actually called

 $ /Applications/MATLAB_R2015b.app/bin/matlab -nodesktop -nosplash -r "foo" 

I recommend that you place the matlab bash script in /usr/local/bin and make sure that /usr/local/bin is in your PATH . The /usr/local/ directory is for user-installed scripts, not system scripts. You can check which directories are in PATH with

 $ echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin 

and you should see a result similar to the above with /usr/local/bin present. bash script must also be executable. You can set this with

 $ sudo chmod +x /usr/local/bin/matlab 

Note OS X El Capitan places strong restrictions on where scripts can be installed using the new System Integrity Protection feature.

Creating a symlink to matlab

Another method similar to creating a bash script is to create a symbolic link to the matlab executable. This should again be placed in /usr/local/bin

 $ cd /usr/local/bin/ $ ln -s /Applications/MATLAB_R2015b.app/bin/matlab matlab 

Also for this method you need to make sure /usr/local/bin is in your PATH .

Adding matlab to PATH

An alternative method is to simply add the directory where the matlab executable is located on your PATH . You can do this by modifying the .bash_profile (or .bashrc ) .bashrc . Your .bash_profile file is located in your home directory in ~/.bash_profile . It is executed every time the user opens the terminal. To add matlab to PATH just add

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

to him. Now you can call matlab with

 $ matlab -nodesktop -nosplash -r "foo" 

and this will find the matlab executable in /Applications/MATLAB_R2015b.app/bin/ and call it using

 $ /Applications/MATLAB_R2015b.app/bin/matlab -nodesktop -nosplash -r "foo" 

After modifying the .bash_profile file, you need to reload it with

 $ source ~/.bash_profile 

or restart the terminal for the changes to take effect.

Note I prefer to modify the .bashrc instead of the .bash_profile , because I use .bashrc for Linux too. I installed the .bash_profile file to download the .bashrc

 $ cat .bash_profile # Load .bashrc if it exists test -f ~/.bashrc && source ~/.bashrc 

Note If you want matlab be available for each user, and not just for the user, you need to add

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

for the system-wide /etc/profile file.

Creating an alias for matlab

The last method I will talk about is to create an alias for matlab . We do this by modifying our .bash_profile (or .bashrc ) file again and adding

 alias matlab="/Applications/MATLAB_R2015b.app/bin/matlab" 

to him. Again, after making the changes, we need to restart it with

 $ source ~/.bash_profile 

or restart the terminal for the changes to take effect. And, if you want matlab be available to every user, not just the user, you need to modify the system-wide /etc/profile file.

Running matlab from terminal

Now that we have configured matlab to conveniently execute from the terminal using a simple command

 $ matlab 

we can look at the execution of scripts. To execute the Matlab script, we must first be in the directory where the script is located, or it can be in our Matlab PATH . I assume this is not in your way and therefore we cd to the correct directory

 $ cd /path/to/foo.m 

Now, to run matlab without a desktop, MathWorks tells us to use -nojvm -nodisplay -nosplash , but if we use -nojvm and / or -nodisplay , we will not be able to display graphs. Therefore, we discard -nojvm and replace -nodisplay with -nodesktop and use -nodesktop -nosplash . This will launch Matlab without a display and allow graphics to be displayed. The correct command to execute matlab without a full graphical desktop interface, as well as to display graphs -

 $ matlab -nodesktop -nosplash 

Now you can use the terminal (command line) as the Matlab command window and execute the commands as usual. For example, we could call foo

 >> foo 

Alternatively, we can use the -r option to the matlab executable to send commands to execute Matlab. They must be correctly specified and valid for the Matlab syntax . Therefore, our team run Matlab with our previous parameters and execute script foo.m becomes

 $ matlab -nodesktop -nosplash -r "foo" 

Beyond : If, for example, we were to use

 $ matlab -nodesktop -nosplash -r "foo; exit;" 

(note the use of exit; ), this will start Matlab, execute foo.m , display the graphs, and then exit Matlab, closing the graphs.

+18
source share

I think the bash script ignores command line arguments. Could you try the following?

 /Applications/MATLAB_R2015b.app/bin/matlab -nosplash -nodisplay -r "run foo.m;quit;" 
+1
source share

That should do the trick

 matlab -nodisplay -nodesktop -nosplash -nojvm -r "foo($v1,$v2);exit" 

enjoy

0
source share

You should be able to do

 #!/bin/bash /Applications/MATLAB_R2015b.app/bin/matlab < /path/to/foo.m 

in other words, use the < sign to redirect on Linux.

0
source share

All Articles