Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Control Execution of a Simulation

Start a Simulation

This section explains how to run a simulation interactively using this modelmodel. See Run Simulation Using the sim Command and Control Simulation Using the set_param Command for information on running a simulation from a program, an S-function, or the MATLAB® command line.

To start the execution of a model, from the Simulation menu of the Model Editor, select Run or click the Run button on the model toolbar.

    Note   A common mistake is to start a simulation while the Simulink® block library is the active window. Make sure that your model window is the active window before starting a simulation.

The model execution begins at the start time that you specify on the Configuration Parameters dialog box. Execution continues until an error occurs, until you pause or terminate the simulation, or until the simulation reaches the stop time as specified on the Configuration Parameters dialog box.

While the simulation is running, a progress bar at the bottom of the model window shows how far the simulation has progressed. A Pause command replaces the Run command on the Simulation menu. A Pause command appears on the menu and replaces the Run button on the model toolbar.

Your computer beeps to signal the completion of the simulation.

Pause or Stop a Simulation

Select the Pause command or button to pause the simulation. Once Simulink completes the execution of the current time step, it suspends the simulation. When you select Pause, the menu item and the button change to Continue. (The button has the same appearance as the Run button). You can resume a suspended simulation at the next time step by choosing Continue.

To terminate execution of the model, select the Stop button or the Stop Simulation menu item. Simulink completes the execution of the current time step and then terminates the simulation. Subsequently selecting the Run menu item or button restarts the simulation at the first time step specified on the Configuration Parameters dialog box.

If the model includes any blocks that write output to a file or to the workspace, or if you select output options on the Configuration Parameters dialog box, the Simulink software writes the data when the simulation is terminated or suspended.

Use Blocks to Stop or Pause a Simulation

Using Stop Blocks

You can use the Stop Simulation block to terminate a simulation when the input to the block is nonzero. To use the Stop Simulation block:

  1. Drag a copy of the Stop Simulation block from the Sinks library and drop it into your model.

  2. Connect the Stop Simulation block to a signal whose value becomes nonzero at the specified stop time.

For example, this modelmodel stops the simulation when the input signal reaches 10.

If the block input is a vector, any nonzero element causes the simulation to terminate.

Creating Pause Blocks

You can use an Assertion block to pause the simulation when the input signal to the block is zero. To create a pause block:

  1. Drag a copy of the Assertion block from the Model Verification library and drop it into your model.

  2. Connect the Assertion block to a signal whose value becomes zero at the desired pause time.

  3. Open the Block Parameters dialog box of the Assertion block .

    • Enter the following commands into the Simulation callback when assertion fails field:

      set_param(bdroot,'SimulationCommand','pause'),
      disp(sprintf('\nSimulation paused.'))
    • Uncheck the Stop simulation when assertion fails option.

  4. Click OK to apply the changes and close this dialog box.

The following model uses a similarly configured Assertion block, in conjunction with the Relational Operator block, to pause the simulation when the simulation time reaches 5.

When the simulation pauses, the Assertion block displays the following message at the MATLAB command line.

Simulation paused
Warning: Assertion detected in 'assertion_as_pause/
   Assertion Used to Pause Simulation' at time 5.000000

You can resume the suspended simulation by choosing Continue from the Simulation menu on the model editor, or by selecting the Continue button in the toolbar.

Was this topic helpful?