Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call MATLAB functions from the Linux command line?

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?

like image 717
ablimit Avatar asked Jan 04 '10 18:01

ablimit


People also ask

Can I run MATLAB function from command line?

MATLAB runs the function using the first run command in the list. For example, click Run to run myfunction using the command result = myfunction(1:10,5) . MATLAB displays the result in the Command Window. To run the function using a different run command from the list, click Run and select the desired command.

How do you call MATLAB in Linux?

To start MATLAB® on Linux platforms, type matlab at the operating system prompt. If you did not set up symbolic links in the installation procedure, then type matlabroot /bin/matlab . matlabroot is the name of the folder in which you installed MATLAB.

How do I run a MATLAB script from the command line?

To run a MATLAB script from the the command line, use MATLAB's -r option, as in this example which runs the Matlab script my_simulation. m from the current directory. Note that the MATLAB script that you run should have an exit command in it.


1 Answers

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 
like image 104
Alex Cohen Avatar answered Oct 08 '22 04:10

Alex Cohen