Calling a MATLAB Function from the Terminal or
Perl
Whenever I have to deal with Matlab functions from the terminal I
run into the same problems. The main problem is that you can't see
what really gets to Matlab after passing the string containing the
Matlab code/function. This makes it hard to find the source of the
issue. Sometimes the terminal keeps complaining and sometimes
Matlab itself.
Suppose you'd like to run the following command:
myfunction('somestring',42)
In the terminal, you could run it like this:
matlab -nojvm -nodisplay -r
myfunction('somestring',42)
Never forget to include
-nojvm -nodisplay, otherwise
the whole GUI stuff pops up. However, the shell is not so happy
with that command:
zsh: unknown file attribute
It took me a while to find out what thats supposed to mean: The
brackets have to be escaped.
matlab -nojvm -nodisplay -r
myfunction\('somestring',42\)
The next error message is a lot easier to understand and comes from
Matlab itself:
??? Undefined function or variable 'somestring'
This just means that the single quotes must be escaped as
well.
matlab -nojvm -nodisplay -r
myfunction\(\'somestring\',42\)
If you need to call the function from Perl it is necessary to
escape the backslashes:
system("matlab -nojvm -nodisplay -r
myfunction\\(\\'somestring\\',42\\)")
Don't forget to include the
exit command somewhere at
the end of your Matlab script, otherwise Matlab will keep running
until you quit it manually.