Basically I have an m
file which looks like
function Z=myfunc()
% Do some calculations
dlmwrite('result.out',Z,',');
end
I just want to execute it from the command line without getting into MATLAB. I tried several options (-nodisplay
, -nodesktop
, -nojvm
, -r
, etc.), none of them worked. I end up getting into MATLAB and have to type "quit" to exit.
What is the solution?
Here's a simple solution that I found.
I have a function func(var) that I wanted to run from a shell script and pass it the first argument for var. I put this in my shell script:
That worked like a treat for me. The trick is that you have to use double quotes with the "-r" command for MATLAB and use single quotes in order to pass the bash argument to MATLAB.
Just make sure that the last line of your MATLAB script is "exit" or that you run
I have modified Alex Cohen's answer for my own needs, so here it is.
My requirements were that the batcher script could handle string and integer/double inputs, and that Matlab should run from the directory from which the batcher script was called.
This script can be called like (if it is on your path):
matlab_batcher.sh functionName stringArg1 stringArg2 1 2.0
Where, the final two arguments will be passed as numbers and the first two as strings.
Use:
Make sure
matlabCommand
has an exit as its last line.You could compile
myfile
into a standalone program and run that instead. Use Matlab's compilermcc
for that (if you have it), more information is provided in this question.This answer was copied from my answer to another question.
You can run an arbitrary function from the commandline by passing a command to Matlab, like this:
This will execute the Matlab command
funcname('arg1', 'arg2', 'arg3', 'argN')
. Ergo, all arguments will be passed as strings and your function needs to handle this, but then again, this applies to command-line options in any other language as well.