user3578925 - 4 months ago 16

Linux Question

is there a way to run a matlab script from linux command line?

For instance, I have the following simple script "test.m":

`x = [1,2,3];`

y = [2;3;4];

prod = x*y

disp(prod)

So what I want is to be able to execute that script from the linux command line without opening the matlab GUI or the matlab command line. That is, I expect something like that:

`~$ matlab test.m`

and I expect to see the result of the product on the linux command line.

I know that you can do that with python e.g.,

`~$ python test.py`

but was wondering if the same can be achieved with matlab.

Answer

In order to run a script you can open Matlab (you can prevent run it without the GUI using `-nodisplay`

and `-nodesktop`

flags), then run the script using the `run`

command, and finally close matlab using `exit`

.

You can do all this from a terminal with a single instruction:

```
matlab -nodisplay -nosplash -nodesktop -r "run('path/to/your/script.m');exit;"
```

However Matlab outputs the welcome message to the console before running your script. To get rid of the welcome message just skip the first 11 lines (10 depending on your Matlab version) using `tail -n +11`

So your final instruction will be:

```
matlab -nodisplay -nosplash -nodesktop -r "run('path/to/your/script.m');exit;" | tail -n +11
```

Source (Stackoverflow)

Comments