0

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.

user7431005
  • 3,899
  • 4
  • 22
  • 49

1 Answers1

2

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.

Edric
  • 23,676
  • 2
  • 38
  • 40
  • If I enter 'type matlab' the output is 'matlab is bashed (/usr/bin/matlab)' thus, I do not think that there is an alias. I tested it today on windows and `matlab -batch test` works there. But no luck on Linux so far. – user7431005 Dec 09 '20 at 07:37
  • How strange. The syntax I posted definitely works correctly, and the error you get definitely occurs for me if I use `-r` twice, so something is wrong _somewhere_. – Edric Dec 09 '20 at 09:16
  • Are you testing it on Linux? (Ubuntu 18.04 with R2020a)? – user7431005 Dec 09 '20 at 09:19
  • Yes, I've just checked R2020a using Debian 10. – Edric Dec 09 '20 at 09:53
  • I just tried it on a different computer running R2019a with the same problem. 'matlab -batch pwd' is not working there as well - maybe it has to do with how our IT installed matlab? but I'm unsure – user7431005 Dec 09 '20 at 09:56
  • Maybe try `set -x` in a shell before you start? Might show if there's anything untoward. – Edric Dec 09 '20 at 09:57