user7431005
user7431005

Reputation: 4539

MATLAB R2020a run .m script from terminal

I want to execute a matlab script from the terminal.

Although there are many questions (e.g.: here or here) and answers out there, I still could not find a solution how to get it working.

I'm currently in the same directory as a test.m script and following the answers found on the internet, I have tried to start matlab using:

matlab -nodesktop -nodisplay -nosplash -r test

From the matlab help, I think the right approach would be to use the -batch flag

matlab -batch "test"

but also this does not work.

I also tried several alternatives:

matlab -nodesktop -nodisplay -nosplash -r "test"
matlab -nodesktop -nodisplay -nosplash -r "test.m"
matlab -nodesktop -nodisplay -nosplash -batch "test.m"
matlab -nodesktop -nodisplay -nosplash -r "run('test')"
matlab -nodesktop -nodisplay -nosplash -r "run('test.m')"
matlab -nodesktop -nodisplay -nosplash -r "run('/absolut/path/test')"

However, I always get the following error message:

                           < M A T L A B (R) >
                  Copyright 1984-2020 The MathWorks, Inc.
              R2020a Update 3 (9.8.0.1396136) 64-bit (glnxa64)
                                May 27, 2020

 
To get started, type doc.
For product information, visit www.mathworks.com.
 
Warning: Command line argument -r cannot be combined with subsequent -r
argument. 

What is working on Linux is

matlab -nodisplay < test.m

Edit: I tested it today on windows and

matlab -batch test 

works on windows.

Upvotes: 0

Views: 1801

Answers (1)

Edric
Edric

Reputation: 25140

The error message you're getting there indicates that somehow you've ended up with two -r specifications, as if you'd called matlab -r test -r test. Are you sure you don't have an alias or similar for matlab?

In any case, as per the doc, the -r is no longer recommended, you should use -batch, and the argument needs to be a statement, not a path to a file. So, you should use

matlab -sd /absolut/path -batch test

The -sd parameter sets the starting directory for MATLAB so it can find "test.m". With -batch, the -nodesktop, -nodisplay, and -nosplash are implied.

Upvotes: 3

Related Questions