Skip to content

Instantly share code, notes, and snippets.

@dgleich
Created June 18, 2013 14:19
Show Gist options
  • Save dgleich/5805712 to your computer and use it in GitHub Desktop.
Save dgleich/5805712 to your computer and use it in GitHub Desktop.
Command to call Matlab as a script
function load_file(filename)
f = load(filename);
fprintf('%i\n',sum(f))
#!/bin/sh
# This version shows additional error output
script=`basename "$1" .m` # this will return $1 unless it ends with .m
if [ "$script" = "$1" ]; then
cmdterm=,
else
cmdterm=\;
fi;
shift # remove the first argument
args="$@"
argsq=`echo $args | tr '"' "'"`
matlab -nodisplay -r "disp('BEGIN>>'); try, $script $argsq $cmdterm catch me, fprintf(2,getReport(me)); exit(1); end, exit(0)" -nosplash | sed -e '1,/^BEGIN>>$/ d'
./matlabcmd load_file.m mydata.txt
./matlabcmd load_file mydata.txt # will display the output
./matlabcmd "randn(5)" # will display the output
./matlabcmd with_error.m # shows the error
./matlabcmd "randn(5) + randn(6)" # shows the error
function with_error
a = zeros(6);
b = zeros(7);
c = a + b;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment