30

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ablimit
  • 2,301
  • 6
  • 27
  • 41
  • 1
    From MathWorks: [How do I run MATLAB in batch mode on a UNIX machine? ](http://www.mathworks.com/support/solutions/en/data/1-15HNG/index.html) – Adam Goode Jan 04 '10 at 18:24

8 Answers8

25

MATLAB can run scripts, but not functions from the command line. This is what I do:

File matlab_batcher.sh:

#!/bin/sh

matlab_exec=matlab
X="${1}(${2})"
echo ${X} > matlab_command_${2}.m
cat matlab_command_${2}.m
${matlab_exec} -nojvm -nodisplay -nosplash < matlab_command_${2}.m
rm matlab_command_${2}.m

Call it by entering:

./matlab_batcher.sh myfunction myinput
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex Cohen
  • 486
  • 4
  • 3
  • What I did is just: matlab -nojvm -nodisplay -nosplash < my_mfile.m >/dev/null 2>/dev/null Anyway it prints some error message, but result is right. – ablimit Jan 04 '10 at 21:37
  • sorry, input from the command line: $1 is the function name, $2 is the input for the function – Alex Cohen Jan 04 '10 at 22:57
  • @AlexCohen : sorry to ask , but I just read through this post. I did not understand much though. Could you please help me here? I want to run matlab from c++. Am lanning to execute command line commands from c++ to invoke the matlab function. wat is this script actually and How am i supose to interpret it? – Lakshmi Narayanan Jan 25 '13 at 15:53
  • 12
    This is factually incorrect. Matlab *can* run functions from the commandline. Try `matlab -nodisplay "funcname arg1 arg2 arg3 argN"`. The only limitation is that all arguments will be strings. – gerrit Feb 06 '13 at 09:26
  • 8
    @gerrit. Arguments can also be numbers if you add parenthesis: ``matlab -r "myfunc(1,2);exit"`` – nimrodm Jun 27 '13 at 08:44
  • 1
    @nimrodm true. I usually want to interface it to a (bash) shell-script, and then `matlab -nodisplay "funcname $@"` limits functionality to the former. – gerrit Jun 27 '13 at 10:19
  • how to pass the output to another program. directing the output to a file (`matlab -nodisplay -nosplash -r "fun1(arg1),exit"`)prints everything including the shell prompt symbol (`>>`) – WYSIWYG Oct 23 '13 at 11:41
22

Use:

matlab -nosplash -nodesktop -logfile remoteAutocode.log -r matlabCommand

Make sure matlabCommand has an exit as its last line.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Douglas Forman
  • 221
  • 2
  • 2
12

You can call functions like this:

matlab -r "yourFunction(0)"

David Doria
  • 9,873
  • 17
  • 85
  • 147
7

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:

matlab -nodesktop -nosplash -r "func('$1')"

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

matlab -nodesktop -nosplash -r "func('$1'); exit"
DML
  • 71
  • 1
  • 1
  • Note that, at least in my setup, using single quotes around $1 passes in the environment variable as a string, and using no quotes around $1 passes it in as a number. Also, in my setup, if func is a function .m file, you don't need to put exit in the double quotes. – Grittathh May 23 '13 at 22:01
3

You can run an arbitrary function from the commandline by passing a command to Matlab, like this:

matlab -nodisplay -r "funcname arg1 arg2 arg3 argN"

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.

gerrit
  • 24,025
  • 17
  • 97
  • 170
1
nohup matlab -nodisplay -nodesktop -nojvm -nosplash -r script.m > output &
Florent
  • 12,310
  • 10
  • 49
  • 58
Max
  • 459
  • 5
  • 4
0

You could compile myfile into a standalone program and run that instead. Use Matlab's compiler mcc for that (if you have it), more information is provided in this question.

This answer was copied from my answer to another question.

Community
  • 1
  • 1
quazgar
  • 4,304
  • 2
  • 29
  • 41
0

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.

#!/bin/bash

matlab_exec=matlab

#Remove the first two arguments
i=0
for var in "$@"
do
 args[$i]=$var
 let i=$i+1
done
unset args[0]

#Construct the Matlab function call
X="${1}("
for arg in ${args[*]} ; do
  #If the variable is not a number, enclose in quotes
  if ! [[ "$arg" =~ ^[0-9]+([.][0-9]+)?$ ]] ; then
    X="${X}'"$arg"',"
  else
    X="${X}"$arg","
  fi
done
X="${X%?}"
X="${X})"

echo The MATLAB function call is ${X}

#Call Matlab
echo "cd('`pwd`');${X}" > matlab_command.m
${matlab_exec} -nojvm -nodisplay -nosplash < matlab_command.m

#Remove the matlab function call
rm matlab_command.m

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.

Steven
  • 589
  • 2
  • 6
  • 11