17

I am going to run a Matlab program in a remote Linux server using SSH. I was wondering how to run Matlab in Linux with only command line, which means there is no graphical environment?

Thanks.

Lei
  • 432
  • 2
  • 5
  • 10

5 Answers5

25

Start MatLab with the following flags

matlab -nodesktop -nojvm -nosplash
  • -nodesktop prevents the desktop

  • -nojvm prevents starting of the java virtual machine

  • -nosplash prevents the start-up splash screen.

Note, that, as Li-aung Yip noted in the comments, Mathworks does not recommend to use the -nojvm flag.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
H.Muster
  • 9,297
  • 1
  • 35
  • 46
  • What do the `-nojvm` and `-nosplash` flags do? – Li-aung Yip Jun 15 '12 at 08:09
  • `-nosplash` prevents the start-up splash screen and `-nojvm` prevents starting of the java virtual machine. – H.Muster Jun 15 '12 at 08:17
  • 3
    I googled; `-nodesktop` and `-nojvm` are slightly different. [Here's an explanation by Michael Katz of The Mathworks.](http://blogs.mathworks.com/community/2010/02/22/launching-matlab-without-the-desktop/) – Li-aung Yip Jun 15 '12 at 08:25
  • I did not claim that `-nodesktop` and `-nojvm` are the same (that's why I listed both flags). However, reading your link I realized that, opposed to what I was thinking before, the JVM is used for more than just the eye-candy (+1 for your comment). Thus, depending on the functions you use, you might need the JVM also in no-desktop mode. I never encountered such a case, though... – H.Muster Jun 15 '12 at 08:30
  • It could happen when you call Java methods/classes from your Matlab code, but don't use figures. Anyhow, good answer, (+1) – Andrey Rubshtein Oct 03 '12 at 17:28
  • 1
    Note that the JVM is needed by the Parallel Computing Toolbox, among others, so if you intend to use parfor or spmd then you must enable the JVM (i.e. don't use -nojvm) – Normadize Nov 14 '13 at 19:25
9
matlab -nodisplay

See here about -nodisplay.

Then -nodesktop and -nosplash are unnecessary. They don't make sense in text mode.

It's probably not a good idea to add -nojvm unless you have a separate good reason to do so. Without the JVM, you lose some functionality whose absence might lead to confusion later on. Source: same link as above. On top of -nodisplay, it doesn't make your non-graphical Matlab session any less graphical.


Here are a couple of ways to run commands non-interactively.

Way 1:

matlab -nodisplay < myScript.m

Put exit as e.g. the last command in myScript.m.

Way 2:

matlab -nodisplay -r "try, myFunction(); catch e, disp(getReport(e)), exit(7), end, exit()" 

The second way is preferable, because e.g. if there is an error in the middle of the code, then the second way will print an error message and exit with a non-zero code. Whereas the first way is equivalent to typing the commands in directly, regardless of what Matlab says (which might be error messages).

In case the next question is "how to suppress the welcome message in text-mode Matlab?", it seems there is NO good way to get rid of it.

Community
  • 1
  • 1
Evgeni Sergeev
  • 22,495
  • 17
  • 107
  • 124
  • 2
    Since R2019a, there is a `-batch` option specifically for running a script non-interactively. It avoids all the issues with `-r` (which required the try/catch and exit statements), and also suppresses the banner. `-r` is no longer recommended since that release. – Cris Luengo Aug 27 '20 at 02:25
7

The command is matlab -nodesktop.

http://www.mathworks.de/help/techdoc/ref/matlabunix.html

Herr von Wurst
  • 2,571
  • 5
  • 32
  • 53
0

Putting the other answers together plus some more error handling, save the following as an executable file matlab-headless:

#/usr/bin/env bash

# restore the "sane" state of the terminal at the end
# otherwise interrupting the process may leave the terminal messed up
trap 'stty sane' EXIT
command="try, $1;, catch e, stack=getReport(e); fprintf(1, '%s\n', stack);, end, exit;"

# tail gets rid of the welcome message banner
matlab -nodisplay -nodesktop -nosplash -r "$command" | tail -n +11

Now you can run a Matlab command as:

matlab-headless "somecommand('bla', 42)"
isarandi
  • 3,120
  • 25
  • 35
  • See [my comment above](https://stackoverflow.com/questions/11046470/run-matlab-in-linux-without-graphical-environment#comment112479502_38501809). You should no longer be using `-r`. Now you just do `matlab -batch myscript`. – Cris Luengo Aug 27 '20 at 02:28
0

Depending of your need, an alternative is to do :

matlab -nosplash -nodesktop -wait -log -r "Matlab Script Line 1;Matlab Script Line 2;exit;"

It's the same method used by Azure DevOps Pipeline to log all Matlab Jobs Output to their online CLI Window.

Reference: Self-Hosted Windows Agent

Location: C:\agent\_work\_tasks\RunMATLABCommand_28fdff80-51b4-4b6e-83e1-cfcf3f3b25a6\0.2.15\bin\run_matlab_command.bat

Weltgeist
  • 137
  • 1
  • 1
  • 11
  • This is outdated advice. See my comment above: https://stackoverflow.com/questions/11046470/run-matlab-in-linux-without-graphical-environment#comment112479502_38501809 – Cris Luengo Jan 09 '21 at 01:54
  • @CrisLuengo, Still using R2017B at work. Plus my comment isn't that much about using -r flags and more about using (-wait & log vs -nojvm). I'm running it on docker win2016 serv, and I found without using theses, the CLI jumps a line and runs everything in the background and gives back CLI controls. When I use these, it stop CLI until process execution, and print the log that was on the other matlab java gui pop-up. Does -batch combine these flags behavior? – Weltgeist Jan 09 '21 at 06:00
  • Yes, it does. `-wait` is a Windows-only option, on Linux it is the shell that determines if the program runs in the background (no wait) or not (wait). – Cris Luengo Jan 09 '21 at 15:39