Main Content

Build Models from a Windows Command Prompt Window

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 (as done in a batch file), you can control MATLAB start up with a number of command-line arguments.

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

To start MATLAB from a Command Prompt window, use these steps:

  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 your overall build process. Such a process can generate code from one or more Simulink® models, then use your makefile to compile custom code and generated code.

This batch file sets the MATLABROOT environment variable, sets the PATH environment variable to include MATLABROOT, and starts MATLAB with an input script argument %1 and a logfile argument.

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 batch file named mat.bat

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

Create a MATLAB script myFilesToBuild.m


Create a MATLAB script my_rtwdemo_counter_builder.m


Create a MATLAB script my_rtwdemo_rtwintro_builder.m


Run the batch file

From the Windows Start menu, open a Command Prompt window, change folders 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 surrounded 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