How to Call Matlab Functions from the Linux Command Line

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 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

Running matlab function from CLI

As the error says, you're calling mymax as if it were a function (by providing input arguments); however, it is a script which can't accept input arguments. You have two options

Make it Function

function mx = mymax(n1, n2)
mx = n1;

if n2 > mx
mx = n2;
end
end

Then from the command line

$ matlab -nodisplay -nodesktop -nosplash -r "mymax(2,4); exit"

Initialize n1 and n2

Your script relies on the variables n1 and n2 existing in the global workspace. You will need to define these variables prior to executing the script.

The -r flag allows you to specify any MATLAB command at the command line to be run. You can initialize the values of n1 and n2 by simply inserting those commands before calling the script (just like you would in a typical MATLAB session).

$ matlab -nodisplay -nodesktop -nosplash -r "n1 = 2; n2 = 4; mymax"

Missing output

The reason that you are not seeing any computed values is because you aren't actually returning them or doing anything useful with them. You could add a disp statement if you want to see these values. Or if you need them for something else, you can always write them out to a file that is accessible by the other software.

The other option is to write all command window output to a log file using the -logfile option.

$ matlab -nodesktop -nodisplay -nosplash -logfile log.txt -r "mymax(2,4); exit"

Running Matlab function from linux command line -- syntax error?

The statement has to come right after the -r switch:

matlab -nodisplay -nojvm -r "checkMembraneSpline(10,1000,'',100,500,2.5); catch; end; quit"

If you have a newer version of MATLAB, use the -batch switch instead:

matlab -nojvm -batch "checkMembraneSpline(10,1000,'',100,500,2.5);"

With this new switch you don’t need to have an exit call, it always quits after competing the statement. Therefore, it is also not necessary to catch errors. Output is put into the terminal by default. Much simpler!

Reference: https://www.mathworks.com/help/matlab/ref/matlablinux.html

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.

Re : Open Matlab function from command line linux

You need to change your b_exec back to matlab_exec, or you need to change

${matlab_exec} -nojvm ...

to

${b_exec} -nojvm ...

Either way, you need to make it consistent.

Matlab script call from command line first calls startup?

Yes, this is expected behaviour. MATLAB executes startup.m when it starts up, whether it's started in the usual way, or whether it's started from the command line with -r and a command.



Related Topics



Leave a reply



Submit