Open the model vdp
.
Create a Simulation
object to represent a simulation of the model.
The model vdp
is small and simple and simulates very fast. Configure the simulation to run at a slower pace relative to real or wall clock time so the simulation runs long enough for you to interact with it. This code configures the simulation to run at a rate of approximately two simulation seconds per one second of real time.
Configure the simulation to run to a stop time of 40 seconds.
Check the simulation status by using the Status
property. Before you start or initialize the simulation, the simulation status is inactive
.
You can initialize the simulation without starting it. Initializing the simulation compiles the model and sets up resources, such as memory, for running the simulation. After you initialize the simulation, the simulation status is initialized
. The Simulink® Editor locks to prevent modifications to the model or simulation by anything other than the Simulation
object that controls the simulation.
Start the simulation and check the simulation status. Once the simulation starts, the status is running
.
Because simulation execution does not block the MATLAB® command prompt, this example uses the MATLAB® pause
function to mimic an interactive workflow. This pause
command pauses the execution of the script to allow the simulation to run for five seconds.
Pause the simulation using the pause
function for the Simulation
object and check the simulation status. While paused, the simulation status is paused
.
The plot in the Scope block updates during the simulation. Logged data also streams to the Simulation Data Inspector.
Check the simulation time. While the script was paused for five seconds, the simulation advanced to a simulation time of approximately 10 seconds. The Time
property value is not exactly 10 because:
Use the pause
function to pause a running simulation on demand. To pause the simulation at or after a specific simulation time, use the step
function with the PauseTime
argument.
For example, advance the simulation through a simulation time of 25 seconds.
Check the simulation time. The simulation pauses at the top of the first major time hit with a simulation time greater than or equal to the specified pause time.
Because the simulation pauses at the top of the major time hit, output values are not available for the current simulation time. Check the time of the last value in the simulation outputs.
The status bar in the Scope window and the status bar in the Simulink® Editor both indicate the simulation time of the last output calculation.
In the Simulink Editor, the asterisk next to the time indicates that the current simulation time is ahead of the time the status bar reports.
Advance the simulation through the current major time hit.
The output results are now available for the time hit.
To continue the simulation, use the resume
function.
Because simulation execution does not block the MATLAB® command prompt, this example uses the MATLAB® pause
function to mimic an interactive workflow. This pause
command pauses the execution of the script to allow the simulation to run for five seconds.
To stop the simulation before the simulation completes, use the stop
function.
out =
Simulink.SimulationOutput:
tout: [97x1 double]
yout: [1x1 Simulink.SimulationData.Dataset]
SimulationMetadata: [1x1 Simulink.SimulationMetadata]
ErrorMessage: [0x0 char]
Check the simulation status. Because the simulation did not enable fast restart, stopping the simulation also terminates the simulation. After stopping, the simulation status is inactive
.
The stop
function returns the Simulink.SimulationOutput
object that contains the simulation results. You can also access the results from the SimulationOutput
property of the Simulation
object.
outprop =
Simulink.SimulationOutput:
tout: [97x1 double]
yout: [1x1 Simulink.SimulationData.Dataset]
SimulationMetadata: [1x1 Simulink.SimulationMetadata]
ErrorMessage: [0x0 char]
When you run another simulation using the Simulation
object sm
, the results from the current simulation overwrite the SimulationOutput
property.