9

Just for the sake of concreteness, consider the following super-simple Python script, in a file called add_em:

#!/usr/bin/env python
# script name: add_em

from sys import argv

x = int(argv[1])
y = int(argv[2])
x_plus_y = x + y
print '%d' % x_plus_y

I can now run this script, and pass arguments to it, from the Unix command line, like this:

% python add_em 3 8
11

If I make the script executable, I don't even need to mention python on the command line:

% chmod +x add_em
% add_em 41 -29
12

Can someone show me how to write (and run) a MATLAB script so that it performs exactly like the script above does? In particular, it must be able to read its arguments from the Unix command line (as opposed to, e.g., the MATLAB GUI's "command line"), and print their numerical sum to standard output.

NOTE: this script need not be "stand-alone"; IOW, one may assume that MATLAB is locally installed, and even that one can mention matlab in the command line (analogously to the first form above, where the python interpreter is explicitly invoked on the command line).

Thanks!

PS: Needless to say, this script is the antithesis of "robust", but my aim was to produce a readily conveyed example.

gevang
  • 4,994
  • 25
  • 33
kjo
  • 33,683
  • 52
  • 148
  • 265
  • There is an answer to your question here: http://stackoverflow.com/questions/3335505/how-can-i-pass-command-line-arguments-to-a-standalone-matlab-executable-running – piokuc Aug 31 '12 at 22:31
  • ***No***, that question is for a *standalone* (meant to be run with no locally installed copy of MATLAB). That's why I ***emphasized*** in my question that I do not require this. In particular, I ***do not*** want to compile the MATLAB script. – kjo Aug 31 '12 at 22:48

1 Answers1

10

You can have a MATLAB function doing what you want inside add_em.m

function add_em(x, y)
x_plus_y = x + y;
disp(x_plus_y);
exit;

and then call it from Unix command line using the -r switch. Example:

matlab -nodesktop -nojvm -nosplash -r "add_em(3, 8)" 

The - options suppress desktop, java and splash so the script/function will be executed without additional overhead.

You can additionally suppress the MATLAB welcome/copyright message by redirecting output to a logfile (for any computations) or tailing, for example, the output in order to get something printed on terminal

matlab -nosplash -nodesktop -nojvm -r "add_em(3, 8)" | tail -n 3

Update: Just found out this post/answers with relevant info: suppress start message of Matlab

Community
  • 1
  • 1
gevang
  • 4,994
  • 25
  • 33
  • 4
    Can you clarify something about this one? How does MATLAB know where to find the .m file defining the function? Do you need to put it somewhere specific for this to work? – Oblivious Sage Dec 17 '12 at 23:31
  • More information on the input arguments of `matlab` can be found on https://se.mathworks.com/help/matlab/ref/matlablinux.html. – Rantanplan Sep 26 '21 at 08:19
  • With `-r`, you can pass all kinds of Matlab statements along with your script name, e.g. to populate your workspace: `matlab -r "a=5;myscript"`will first create the variable a and then, run your script myscript.m. – Rantanplan Sep 26 '21 at 08:26