MATLAB Answers

run matlab function with arguments on linux terminal

1,194 views (last 30 days)
Ayoub
Ayoub on 22 May 2012
Commented: Walter Roberson on 20 Jun 2019
Hello, do you have any idea how to run matlab functions from the linux shell specifying their arguments? what i tried was: { matlab -r myfunction(argument1,argument2) } but it doesn't work oO

  2 Comments

Yekaterina Gilbo
Yekaterina Gilbo on 14 Jul 2017
Hi Ayoub, what is the purpose of the "-r" and also how do you know what to even type on the terminal (what should I look up for this?) Thanks!
Walter Roberson
Walter Roberson on 14 Jul 2017
"-r" followed by something indicates that MATLAB should take what follows as a command to run.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 22 May 2012
matlab -r 'myfunction(argument1,argument2)';
Or safer:
matlab -r 'try myfunction(argument1,argument2); catch; end; quit'

  0 Comments

Sign in to comment.

More Answers (4)

Thomas
Thomas on 22 May 2012
Adding to Walter's answers above..
you could also try
matlab -r -nodisplay -nojvm 'myfunction(argument1,argument2)';
-no display removes the Xdisplay and -nojvm starts matlab without hte Java virtual machine.
you could also try
matlab -r -nodesktop -nojvm 'myfunction(argument1,argument2)';
or use the abovewith the try-catch loop as suggested by Walter..

  0 Comments

Sign in to comment.


USMAN
USMAN on 15 Jul 2014
I would rearrange the sequence of input arguments as follows:
matlab -nodisplay -nojvm -r 'FunctionName Arg1 Arg2; exit;'
exit will also exit Matlab and bring control back to shell

  1 Comment

Patrik Ek
Patrik Ek on 2 Jan 2015
Which is the only way to use the -r argument. Good add!

Sign in to comment.


Ayoub
Ayoub on 29 May 2012
thankyou guys :)

  0 Comments

Sign in to comment.


Martina Audagnotto
Martina Audagnotto on 21 May 2018
Edited: Walter Roberson on 22 May 2018
Hi,
I have a similar question regarding running matlab with arguments on linux terminal. I want to run my matlab code on GPU and if I set the number of GPU manually the code run, while if i give the number of GPU as a variable of my function I get the following error:
Not enough input arguments.
This is how I run the function:
matlab -nodesktop -r "nodes=2; averaging_filament='single_filament_averaging_reikaparam_is40'; folder='polarity_TRIAL'; run streamline_tomo_GPU.m" ;
and inside the code:
GPU=[nodes];
No idea why is it not running :(

  3 Comments

Walter Roberson
Walter Roberson on 22 May 2018
I recommend converting streamline_tomo_GPU into a function with nodes as its parameter. Alternately, convert
GPU=[nodes];
to
GPU=evalin('base','nodes');
Lucademicus
Lucademicus on 20 Jun 2019
According to the matlablinux documentation, you should not use the option
-r
but instead use the option
-batch
I've tested this on MATLAB R2018b.
The command
matlab -nodisplay -nojvm -r 'somefunction(someargument); anotherfunction(anotherargument);'
did not run anotherfunction(). It did when I changed -r to -batch!
Walter Roberson
Walter Roberson on 20 Jun 2019
-batch is a new option as of R2019a. -r still works.

Sign in to comment.

Sign in to answer this question.