0
0
SimulinkHow-ToBeginner · 3 min read

How to Set Simulation Time in Simulink Quickly and Easily

In Simulink, set the simulation time by opening the Simulation tab and entering values in the Start time and Stop time fields. These fields control when the simulation begins and ends.
📐

Syntax

To set simulation time in Simulink, use the following syntax in the MATLAB command window or script:

  • set_param(modelName, 'StartTime', 'startValue') sets the simulation start time.
  • set_param(modelName, 'StopTime', 'stopValue') sets the simulation stop time.

Replace modelName with your Simulink model's name, and startValue and stopValue with numeric strings representing time in seconds.

matlab
set_param('myModel', 'StartTime', '0')
set_param('myModel', 'StopTime', '10')
💻

Example

This example shows how to set the simulation time for a model named myModel to start at 0 seconds and stop at 5 seconds using MATLAB commands.

matlab
modelName = 'myModel';
load_system(modelName);
set_param(modelName, 'StartTime', '0');
set_param(modelName, 'StopTime', '5');
sim(modelName);
Output
Simulating model 'myModel' from 0 to 5 seconds...
⚠️

Common Pitfalls

Common mistakes when setting simulation time include:

  • Entering non-numeric or negative values in StartTime or StopTime.
  • Setting StopTime less than StartTime, which causes errors.
  • Forgetting to save or load the model before setting parameters.

Always ensure your model is loaded and the times are valid numbers with StopTime greater than StartTime.

matlab
modelName = 'myModel';
load_system(modelName);
% Wrong: StopTime less than StartTime
set_param(modelName, 'StartTime', '10');
set_param(modelName, 'StopTime', '5'); % This will cause an error

% Correct way
set_param(modelName, 'StartTime', '0');
set_param(modelName, 'StopTime', '10');
📊

Quick Reference

Summary tips for setting simulation time in Simulink:

  • Use the Simulation tab in Simulink GUI to set Start time and Stop time directly.
  • Use set_param in MATLAB scripts for automation.
  • Ensure Stop time is greater than Start time.
  • Simulation time is in seconds by default.

Key Takeaways

Set simulation time in Simulink via the Simulation tab or MATLAB commands.
Use set_param with 'StartTime' and 'StopTime' to control simulation duration programmatically.
Always ensure StopTime is greater than StartTime to avoid errors.
Simulation times are specified in seconds as strings.
Load your model before setting simulation parameters in scripts.