MATLAB Examples

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. Change folders to $MATLABROOT\bin. $MATLABROOT is the MATLAB root folder.
  3. In the Command Prompt window, 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\R2015b"
START matlab.exe -r %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.

Tip: Omitting the semicolon (;) from the rtwbuild 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. Some options to consider include:

  • Suppress the MATLAB splash screen on startup with the -nosplash argument.
  • Suppress the MATLAB window when running with the -nodesktop -minimize arguments.
  • Provide command-line input to the input script or function selected with the -r argument.

For example, you can call a function myfile.m, which accepts two arguments:

matlab -r 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 -r myfile('hello','world')

See Also