How to Run MATLAB Script From Command Line

Sheeraz Gul Feb 02, 2024
How to Run MATLAB Script From Command Line

The MATLAB scripts with the extension .m can be compiled using the command prompt in Windows or other operating systems.

This tutorial demonstrates how to run MATLAB scripts from the command line in Windows.

Run MATLAB Script From Command Line

First, make sure that MATLAB is added to the path of environment variables:

Environment Variable

Once MATLAB is added to environment variables, we can run it through the command prompt.

Let’s try to run the following new.m file from the command prompt.

disp('This matlab program is running from command line')

Run the following in the command prompt. Add your path and file name in the run() command.

matlab -nodisplay -nosplash -nodesktop -r "run('C:\Users\Sheeraz\matlab\new.m');

Command Line

Output:

MATLAB Command Window

The command above will open the command window with the file output, but this command window will remain open. We can add the exit command to close it on successful execution.

matlab -nodisplay -nosplash -nodesktop -r "run('C:\Users\Sheeraz\matlab\new.m'); exit

This command will run the MATLAB file and close the command window on successful execution.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook