Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a matlab program with arguments

Tags:

matlab

I have a matlab file that takes in a file. I would like to run that program in the matlab shell, such as prog. I need to implement it so that it takes a number of arguments, such as "prog filename.txt 1 2 which would mean that i can use filename.txt and 1 2 as variables in my program.

Thank you!

like image 633
bubbles Avatar asked Jan 24 '12 01:01

bubbles


1 Answers

Once your function is written in a separate file, as discussed by the other answer you can call it with a slightly more complicated setup to make it easier to catch errors etc.

There is useful advice in this thread about ensuring that Matlab doesn't launch the graphical interface and quits after finishing the script, and reports the error nicely if there is one.

For example:

matlab -nodisplay -nosplash -r "try, prog(1, 'file.txt'), catch me, fprintf('%s / %s\n',me.identifier,me.message), exit(1), end, exit(0)"

The script given to Matlab would read as follows if line spaces were added:

% Try running the script
try
    prog(1, 'file.txt')
catch me
% On error, print error message and exit with failure
    fprintf('%s / %s\n',me.identifier,me.message)
    exit(1)
end
% Else, exit with success
exit(0)
like image 120
goldfinch13 Avatar answered Oct 10 '22 05:10

goldfinch13