Matlab Setting Simulink Simulation Stop Time

| Comments

Running the Simulink model model.mdl from matlab for a specified time:

sim('model', stoptime)

Where stoptime is a number. To mix with other options such eg. SimulationMode, things get a little trickier.

sim( 'model', 'SimulationMode', 'normal')

you would expect:

sim( 'model', 'SimulationMode', 'normal', 'StopTime', 1)

but this results in :

Invalid setting in block_diagram 'model' for parameter 'StopTime'

The trick is that parameter, value pairs must be strings, what you actually need to do is:

sim( 'model', 'SimulationMode', 'normal', 'StopTime', num2str(1) )

Or you can create an ParameterStruct,

SimOpts.SimulationMode = 'normal';
SimOpts.StopTime       = num2str(1);
sim( 'model', SimOpts )

Both of these methods change the way in which the to_workspace blocks work, instead of just outputting a variable to the workspace or available in the calling function sim now returns an ‘Simulink.SimulationOutput’ object.

Instead of just accessing ‘simout’ you know need to capture the sim output and use the get command to retrieve the data :

simdata = sim( 'model', SimOpts ) ;
simdata.get('simout' )

Further Info on the sim command.