22

I have been trying to call matlab from Terminal (I have a Mac) and have it simply run a program for me, without display or anything, just the program. This program both displays graphs and writes a text file for me to use. I have found several of the previous answers on this question, and so I have 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

Pretty much every time I try it, I keep getting the same response (when the line just doesn't cause the prompt to say "screw you" and give up): the actual MATLAB program window will open, and it will stay open. It will interact normally. And command line won't do anything until I close out the window. The program I want doesn't run. The window just... sits there.

If it helps, this is how I have matlab coded in my bin:

#!/bin/bash                                                                                             

/Applications/MATLAB_R2015b.app/bin/matlab

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

5
  • Could you try appending exit to your file (foo.m) and trying matlab -nosplash -nojvm -nodesktop -r "foo".
    – Nitish
    Commented Oct 17, 2015 at 18:09
  • Both with and without -nodisplay, it's a no go. Commented Oct 17, 2015 at 19:43
  • And does the script work as expected when called interactively from the matlab gui? Commented Oct 17, 2015 at 20:03
  • 1
    And what is the expected behaviour of a script producing figures if figures are disabled by a non-existent gui? BTW the terminal being unresponsive while matlab runs is perfectly normal, in linux you can get your shell prompt back by putting an ampersand (&) at the end of your command (but killing that terminal process can kill matlab running in it). Commented Oct 17, 2015 at 20:13
  • The script works normally when called from the GUI. Commented Oct 17, 2015 at 20:19

4 Answers 4

38
+50

Your bash script for calling Matlab will not pass any arguments to the Matlab executable. When you type

$ matlab -nodesktop -nosplash -r "foo"

what is actually called is

$ /Applications/MATLAB_R2015b.app/bin/matlab

without the arguments. There are several ways you can fix this whilst retaining the ease of just calling matlab. Alternatively you could call the full path to matlab like

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

Setting Up matlab Executable

Bash Script

Given that you have already written a bash script to call matlab the easiest solution is to alter it to include the $@ bash wildcard like

#!/bin/bash                                                                                             

/Applications/MATLAB_R2015b.app/bin/matlab "$@"

The $@ wildcard passes all of the parameters you use, like -nodesktop -nosplash -r "foo" to the matlab executable so what is actually called now is

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

I recommend you place your matlab bash script in /usr/local/bin and ensure that /usr/local/bin is in your PATH. The /usr/local/ directory is for user installed scripts as opposed to system installed scripts. You can check what directories are in your PATH with

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

and you should see an output similar to the above with /usr/local/bin present. The bash script should 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 via its new System Integrity Protection feature.

Creating a Symlink to matlab

Another method similar to the creation of the bash script is to create a symbolic link to the matlab executable. This again should 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 that /usr/local/bin is in your PATH.

Adding matlab to the PATH

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

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

to it. Now you can call matlab with

$ matlab -nodesktop -nosplash -r "foo"

and this will locate the matlab executable in /Applications/MATLAB_R2015b.app/bin/ and call it with

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

After you modify your .bash_profile file you need to reload it with

$ source ~/.bash_profile

or restart the Terminal for the changes to take affect.

Note: I prefer to modify the .bashrc file instead of .bash_profile because I use .bashrc on Linux too. I have set my .bash_profile file up to load my .bashrc file

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

Note: If you want matlab to be available for every user and not just your user you need to add

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

to the system-wide /etc/profile file.

Creating an Alias for matlab

The last method I'm going to mention is creating an alias for matlab. We do this by again modifying our .bash_profile (or .bashrc) file and appending

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

to it. Again, after making changes we need to reload it with

$ source ~/.bash_profile

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

Executing matlab from the Terminal

Now that we've set up matlab to conveniently execute from the Terminal with the simple command

$ matlab

we can look at actually executing scripts. To execute a Matlab script we first need to be in the directory where the script resides or it could be in our Matlab PATH. I'll assume it is not in your path and so we'll cd to the correct directory

$ cd /path/to/foo.m

Now to execute matlab without the desktop MathWorks tells us to use -nojvm -nodisplay -nosplash but if we use -nojvm and/or -nodisplay we won't be able to display graphs. So we drop -nojvm and replace -nodisplay with -nodesktop and use -nodesktop -nosplash. This will launch Matlab without a display and allow us to display graphs. The correct command then to execute matlab without the full desktop GUI whilst also allowing us to display graphs is

$ matlab -nodesktop -nosplash

Now you can use the Terminal (command prompt) as the Matlab command window and execute commands as normal. For instance we could call foo

>> foo

Alternatively, we can use the -r option for the matlab executable to pass in commands for Matlab to execute. These must be quoted correctly and valid Matlab syntax. So our command to start Matlab with our previous options and to execute the script foo.m becomes

$ matlab -nodesktop -nosplash -r "foo"

Aside: If, for example, we were to use

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

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

4
  • THAAAAAAAANK YOOOOOOUUUU. This was driving me insane. (I wish it would let me award the bounty now.) Commented Oct 20, 2015 at 20:11
  • Yes. The huge amount of model analysis I have to do is vastly expedited by having this around. Thank you incredibly. Commented Oct 20, 2015 at 20:15
  • I actually need command line here. I'm running a perl script that regex search and replaces certain variables in the MATLAB script, then runs it, parses the data appropriately and stores and analyzes it in several different ways. The word "annoying" would not even begin to describe how annoying it would be to do this by hand. Commented Oct 20, 2015 at 20:39
  • Matlab 2019 adds the -batch option for non-interactive execution of script, statement, or function: se.mathworks.com/help/matlab/ref/matlabwindows.html
    – Andreas
    Commented Jan 18, 2022 at 9:17
6

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

/Applications/MATLAB_R2015b.app/bin/matlab -nosplash -nodisplay -r "run foo.m;quit;"
7
  • 1
    Oh, good catch with the script! It might work if matlab was a bash alias instead. Commented Oct 18, 2015 at 8:39
  • Gave a syntax error: -bash: syntax error near unexpected token `&' Commented Oct 19, 2015 at 0:25
  • Now it will pull up Matlab in the command line instead, but now it says "foo.m not found" even though I'm running the line while cd'd into the folder that has foo.m right there. Commented Oct 19, 2015 at 0:51
  • One or more of the following should catch: 1. "addpath /your/script/path;run foo.m;quit;" 2. "cd /your/script/path;run foo.m;quit;" 3. "cd /your/script/path;foo;quit;" 4. "addpath /your/script/path;foo;quit;" Commented Oct 19, 2015 at 0:55
  • Warning: Name is nonexistent or not a directory: /my/path > In path (line 109) In addpath (line 88) Error using run (line 70) foo.m not found. Commented Oct 19, 2015 at 1:08
1

This should do the trick

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

enjoy

0

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 re-direct, in Linux.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.