Join the Stack Overflow Community
Stack Overflow is a community of 6.2 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
    
There is an answer to your question here: stackoverflow.com/questions/3335505/… – 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
up vote 8 down vote accepted

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

share|improve this answer
    
Thanks for a great answer. I wish I could up-vote it more. – kjo Sep 1 '12 at 22:23
2  
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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