How do I call MATLAB from the DOS prompt?

831 views (last 30 days)
I would like to call MATLAB from the DOS prompt without opening it, and have it return a specific output file.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 30 Dec 2016
To start MATLAB from a DOS window running inside Windows, do the following:
1. Open a DOS prompt
2. Change directories to $MATLABROOT\bin
(where $MATLABROOT is the MATLAB root directory on your machine, as returned by typing
matlabroot
at the MATLAB Command Prompt.)
3. Type "matlab"
You can also create your own batch file in the $MATLABROOT\bin directory
NOTE: If you have other commands that follow the call to MATLAB in your batch file, use matlab.exe rather than matlab.bat. If you call on matlab.bat, subsequent commands in the batch file will not get executed.
To do so, use the following instructions:
1. Create a file called mat.bat and place the following line into it:
win $MATLABROOT\bin\matlab.exe
2. Insert $MATLABROOT\bin into the path in the autoexec.bat file.
(where $MATLABROOT is the MATLAB root directory on your machine, as returned by typing
matlabroot
at the MATLAB Command Prompt.)
Now you can type "mat" at the dos prompt and Windows will come up with MATLAB.
You can run MATLAB from the DOS prompt and save the session to an output file by doing the following:
matlab -r matlab_filename_here -logfile c:\temp\logfile
Depending on the directory you are in, you may need to specify the path to the executable. The MATLAB file you want to run must be on your path or in the directory. This MATLAB file can be a function that takes arguments or a script.
When running a script 'myfile.m', use the following command:
matlab -r myfile
When calling a function 'myfile.m' which accepts two arguments:
matlab -r myfile(arg1,arg2)
To pass numeric values into 'myfile.m' simply replace 'arg1' and 'arg2' with numeric values. To pass string or character values into 'myfile.m' replace 'arg1' and 'arg2' with the string or character values surrounded in single quotes. For exampl to pass the string values 'hello' and 'world' into 'myfile.m' use the following command:
matlab -r myfile('hello','world')
Note that the logfile will contain everything that was displayed to the Command Window while the MATLAB file was running. If you want to generate any print files you need to do this in the MATLAB file. You can combine this example with the above one to create a batch file that takes input files and creates output files.
In addition, this will call up an additional instance of the MATLAB command window. If you wish this to exit after the computation is complete, you will need to add the command 'exit' to the end of your MATLAB file. You can suppress the splash screen by adding the -nosplash flag to the above command so it looks like the following:
matlab -nosplash -r mfile -logfile c:\temp\logfile
Although you cannot prevent MATLAB from creating a window when starting on Windows systems, you can force the window to be hidden, by using the start command with the -nodesktop and -minimize options together:
start matlab -nosplash -nodesktop -minimize -r matlab_filename_here -logfile c:\temp\logfile
If you would like to call multiple MATLAB functions using the -r switch you could write a single function which will call each of the other MATLAB functions in the desired order.
Note: Batch files can be called from Windows scheduler in order to run MATLAB commands at specific times. May not work for UNC pathnames.
  5 Comments
Yinjie Yu
Yinjie Yu on 3 Nov 2021
Do you know how to run a file that's not in the current folder, say ../otherfolder/somescript.m ?

Sign in to comment.

More Answers (3)

jerry
jerry on 31 Mar 2014
thank you very much, this is extremely helpful for me!

Jean-sebastien LEUNG
Jean-sebastien LEUNG on 7 Dec 2016
Hello, I've been encountering some issues with your proposed solution to run matlab through a batch file. I've been wanting to run a matlab script using a batch file, then close matlab and then run another matlab script using a batch file and so on. I've managed to do the first and second step without problem, but the third step is giving me troubles as the second execution is executing at the same time as the first. (i.e I get two matlabs running at the same time) I've been using the following command lines in my batch
"C:\Program Files\Matlab\R2009b\bin\matlab.exe" -r display1
"C:\Program Files\Matlab\R2009b\bin\matlab.exe" -r display2
  2 Comments
Stefano Sinigardi
Stefano Sinigardi on 13 Jun 2018
is there any way to instead wait for the first command to execute and then launch another step, in case it is not a second Matlab script but something different?

Sign in to comment.


Oreste Fecarotta
Oreste Fecarotta on 11 Mar 2020
Edited: MathWorks Support Team on 3 Apr 2020
In Windows you can prevent MATLAB from opening new windows and execute a script like a process using Powershell and SSH.
If you install the OpenSSH server then you can access via SSH and write any command there. There are instructions at the Microsoft website below:
Open a new powershell window (win+x -> powershell), then write
ssh username@127.0.0.1
where "username" is the name of your Windows account. The system will require the Windows password. After getting the access, you can type commands according to the Windows shell syntax (as in the command prompt). Type help for a list of commands.
For example type:
start matlab -r matlab_filename_here(arg1,arg2) -logfile log_text.txt
The only difference is that the application you launch will run in the background.
Do not close the powershell window after you launch matlab, or the process will expire.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!