Running a Simulation Programmatically

About Simulating Programmatically

Entering simulation commands in the MATLAB® Command Window or from an M-file enables you to run unattended simulations. You can perform Monte Carlo analysis by changing the parameters randomly and executing simulations in a loop. You can use either the sim command or the set_param command to run a simulation programmatically.

Using the sim Command

The full syntax of the command that runs the simulation is

[t,x,y] = sim(model, timespan, options, ut);

Only the model parameter is required. Parameters not supplied on the command are taken from the Configuration Parameters dialog box settings.

For detailed syntax for the sim command, see the sim command reference page. The options parameter is a structure that supplies additional configuration parameters, including the solver name and error tolerances. You define parameters in the options structure using the simset command. The configuration parameters are discussed in Configuration Sets.

Programmatic Symbol Resolution

When you use the sim command to run a simulation programmatically, you have two options that do not exist with interactive simulation: you can specify a workspace other than the MATLAB base workspace as the last workspace searched in hierarchical symbol resolution, and a workspace other than the MATLAB base workspace as the destination for any data logged or exported during simulation.

Most simulation is interactive, so most Simulink® documentation does not mention these possibilities. For information about substituting some other workspace for the base workspace during programmatic simulation, see the sim command reference page.

Using the set_param Command

You can use the set_param command to start, stop, pause, continue a simulation, update a block diagram, or write all logging variables to the base workspace. The format of the set_param command for this use is

set_param('sys', 'SimulationCommand', 'cmd')

where 'sys' is the name of the system and 'cmd' is 'start', 'stop', 'pause', 'continue', 'update', or 'WriteDataLogs'.

Similarly, you can use the get_param command to check the status of a simulation. The format of the get_param command for this use is

get_param('sys', 'SimulationStatus')

The Simulink software returns 'stopped', 'initializing', 'running', 'paused', 'updating', 'terminating', and 'external' (used with the Real-Time Workshop® product).

Running a Simulation from an S-Function

S-functions can use the set_param command to control simulation execution. A C MEX S-function can use the mexCallMatlab macro to call the set_param command itself.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS