Main Content

Build Models from Windows Command Prompt

This example shows how to build models by using a batch file, entering commands at the Command Prompt in Windows®.

About MATLAB Command-Line (Start Up) Arguments

When you start MATLAB® from a Command Prompt in Windows, you can control MATLAB start up with command-line arguments.

For a description of these command-line arguments, in the Windows Command Prompt, type matlab -help.

To start MATLAB from the Windows Command Prompt:

  1. From the Windows Start menu, open a Command Prompt window.

  2. From the Windows Command Prompt, type: matlab.

Tip: To display the path to the MATLAB root folder, at the MATLAB command prompt type: matlabroot.

Run MATLAB with a Batch File

When you run MATLAB with a batch file, you can:

  • Control MATLAB start up with command-line arguments

  • Run a series of operating system commands (such as source control checkout/commit)

  • Run a series of MATLAB scripts

A batch approach also lets you automate model building. You can generate code from one or more Simulink® models and use your makefile to compile custom code and generated code.

The batch file mat.bat:

  • Sets the MATLABROOT environment variable.

  • Sets the PATH environment variable to include MATLABROOT.

  • Starts MATLAB with an input script argument %1 and a logfile argument.

SET MATLABROOT="C:\Program Files\MATLAB\R2019a"
START matlab.exe -batch %1 -logfile c:\temp\logfile

Note: Customize the MATLABROOT value in the batch file to match your system. The batch file assumes that a c:\temp folder exists on your system.

Create a MATLAB script, for examples, myFilesToBuild.m.


my_counter_builderA.m contains these statements:



my_counter_builderB.m contains these statements:



Run the batch file

From the Windows Start menu, open a Command Prompt window, go to the folder containing the batch file, and type:

mat myFilesToBuild

When you run the batch file with the input MATLAB script, the batch file runs MATLAB and loads, builds, and closes each of the example Simulink models.

Observe the log of MATLAB operations

After the batch file runs, view the c:\temp\logfile file.

Omitting the semicolon (;) from the slbuild line in each script provides more build information in the log file.

Optimize Your Batch File

Use the MATLAB command-line arguments to optimize the batch file. For example:

  • Suppress the MATLAB splash screen on startup with the -nosplash argument.

  • Provide command-line input to the input script or function selected with the -batch argument.

You can call a function myfile.m, which accepts two arguments:

matlab -batch myfile(arg1,arg2)

To pass numeric values into myfile.m, 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 enclosed in single quotes. For example, to pass the string values hello and world into myfile.m, in the Command Prompt window, type:

matlab -batch myfile('hello','world')

Copyright 2007-2019 The MathWorks, Inc.

Related Topics