Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matlab execute script from command linux line

Tags:

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.

like image 810
user3578925 Avatar asked Aug 02 '16 14:08

user3578925


People also ask

How do I run a MATLAB script in Linux terminal?

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. To see the folder, type matlabroot .

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.

How do you run .m file in Linux?

Assuming the Linux server has Matlab installed on it as well, (and that it has also been added to the path on the server), you can run your Matlab scripts from the terminal, simply by entering in the terminal, $ matlab your_script_name. m .

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 I run a Matlab script from the command line?

This tutorial demonstrates how to run MATLAB scripts from the command line in Windows. First, make sure that MATLAB is added to the path of environment variables: Once MATLAB is added to environment variables, we can run it through the command prompt.

How do I run Matlab without a GUI?

The correct command then to execute matlab without the full desktop GUI whilst also allowing us to display graphs is Now you can use the Terminal (command prompt) as the Matlab command window and execute commands as normal. For instance we could call foo

How to close the MATLAB file on successful execution?

We can add the exit command to close it on successful execution. matlab -nodisplay -nosplash -nodesktop -r "run ('C:\Users\Sheeraz\matlab ew.m'); exit This command will run the MATLAB file and close the command window on successful execution.

How to run a MATLAB file in Sheeraz?

matlab -nodisplay -nosplash -nodesktop -r "run ('C:\Users\Sheeraz\matlab ew.m'); exit This command will run the MATLAB file and close the command window on successful execution. DelftStack articles are written by software geeks like you.


2 Answers

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 
like image 73
Sembei Norimaki Avatar answered Sep 20 '22 00:09

Sembei Norimaki


Starting with R2019a, the preferred method would be, for your test.m script:

matlab -batch "test" 

This has several advantages, mainly no need for all the -no flags and MATLAB will exit with non-zero status if test.m (must be on search path) contains an error.

From the documentation page, matlab (Linux):

Execute MATLAB script, statement, or function non-interactively. MATLAB:

  • Starts without the desktop
  • Does not display the splash screen
  • Executes statement
  • Disables changes to preferences
  • Disables toolbox caching
  • Logs text to stdout and stderr
  • Does not display dialog boxes
  • Exits automatically with exit code 0 if script executes successfully. Otherwise, MATLAB terminates with a non-zero exit code.

statement is MATLAB code enclosed in double quotation marks. If statement is the name of a MATLAB function or script, do not specify the file extension. Any required file must be on the MATLAB search path or in the startup folder.

Use the -batch option in non-interactive scripting or command line work flows. Do not use this option with the -r option.

To test if a session of MATLAB is running in batch mode, call the batchStartupOptionUsed function.

Example: -batch "myscript"

like image 30
bdsinger Avatar answered Sep 23 '22 00:09

bdsinger