Matlab Execute Script from Linux Command Line

matlab execute script from linux command line

In order to run a script you can open Matlab (you can prevent run it without the GUI using -nodisplay and -nodesktop flags), then run the script using the run command, and finally close matlab using exit.

You can do all this from a terminal with a single instruction:

matlab -nodisplay -nosplash -nodesktop -r "run('path/to/your/script.m');exit;"

However Matlab outputs the welcome message to the console before running your script. To get rid of the welcome message just skip the first 11 lines (10 depending on your Matlab version) using tail -n +11

So your final instruction will be:

matlab -nodisplay -nosplash -nodesktop -r "run('path/to/your/script.m');exit;" | tail -n +11

How to call MATLAB script from command line?

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.

run matlab script from command line or shell

Yes, you can call matlab from command prompt. In a windows machine it will look like this depending on your matlab installation path:

"C:\Path\to\matlab\matlab.exe" -r matfile.m

But it does open up Matlab gui. I do not know how to run it silently, but including

exit

at the end of your routine will close it automatically.

Matlab: Running an m-file from command-line

A command like this runs the m-file successfully:

"C:\<a long path here>\matlab.exe" -nodisplay -nosplash -nodesktop -r "run('C:\<a long path here>\mfile.m'); exit;"

MATLAB R2020a run .m script from terminal

The error message you're getting there indicates that somehow you've ended up with two -r specifications, as if you'd called matlab -r test -r test. Are you sure you don't have an alias or similar for matlab?

In any case, as per the doc, the -r is no longer recommended, you should use -batch, and the argument needs to be a statement, not a path to a file. So, you should use

matlab -sd /absolut/path -batch test

The -sd parameter sets the starting directory for MATLAB so it can find "test.m". With -batch, the -nodesktop, -nodisplay, and -nosplash are implied.

How to call MATLAB functions from the Linux command line?

MATLAB can run scripts, but not functions from the command line. This is what I do:

File matlab_batcher.sh:

#!/bin/sh

matlab_exec=matlab
X="${1}(${2})"
echo ${X} > matlab_command_${2}.m
cat matlab_command_${2}.m
${matlab_exec} -nojvm -nodisplay -nosplash < matlab_command_${2}.m
rm matlab_command_${2}.m

Call it by entering:

./matlab_batcher.sh myfunction myinput

Matlab - run file without opening GUI, then quit

matlab needs to interpreter to run your commands. you can always end your file with quit to make matlab exit again when finished with your calculations

MATLAB - how to pass argument with command line and run the script

You can do like this:

matlab -r 'dbEval(argument1, ... ,argumentN)';

or

matlab -r 'try dbEval(argument1, ... ,argumentN); catch; end; quit'


Related Topics



Leave a reply



Submit