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.
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 .
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.
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 .
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.
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.
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
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.
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.
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
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
andstderr
- 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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With