Monday, July 15, 2024
HomeMatlabThe best way to Run Simulink Simulation from MATLAB Script? » Man...

The best way to Run Simulink Simulation from MATLAB Script? » Man on Simulink

I just lately ran right into a report of essentially the most visited entries on MATLAB Solutions. I appeared on the ones involving Simulink and thought it could be attention-grabbing to cowl a few of these on this weblog.

Whereas most questions have direct solutions, I feel it is a good alternative to offer extra background and canopy some subtleties not addressed on MATLAB Solutions.

This week, I’m beginning with essentially the most visited:

Earlier than supplying you with my reply to this query, I need to encourage you to go to the documentation web page Run Simulations Programmatically. It covers in particulars into all of the choices accessible and their implications.

Now, let’s go together with my shorter casual model.

To me, simulating a mannequin programmatically boils all the way down to the 4 following choices:

  • Sim with mannequin identify: Passing the mannequin identify to the sim command. It is easy and works for a lot of use circumstances.
  • Sim with Simulation Enter object: Passing a Simulink.SimulationInput object to the sim command. Probably the most versatile and scalable strategy.
  • Simulation object: Launched in R2024a, the Simulation object offers an interface to regulate the simulation execution: begin, step, cease and tune parameters
  • Outdated and Discouraged: Older syntaxes which might be nonetheless accessible for backward compatibility, however that it is best to keep away from

Sim with Mannequin Title

If in case you have a Simulink mannequin that simulates utilizing the Run button, the quickest and easiest option to simulate it from MATLAB might be to name the sim command and move it the mannequin identify.

For instance, I’ve a mannequin saved as suspension.slx.

I can simulate it by first defining the info it wants within the MATLAB workspace after which calling sim:

out = sim(mdl)

All of the logged information is contained inside the variable out, which is a Simulink.SimulationOutput object. For instance, if I need to plot the speed computed by the Integrator block, I can execute:


Should you do that together with your mannequin and sim returns a vector as an alternative of a Simulation Output object, see the Outdated and Discouraged part beneath.

Sim with Simulation Enter object

Whereas the above strategy works in lots of circumstances, I like to recommend a barely totally different strategy. As described by Reid Spence in this remark, specifying a Simulink.SimulationInput object and passing it to the sim command provides quite a lot of benefits:
  • Explicitly specify variables, indicators, states and parameters used for the simulation
  • Simple transition to parallel simulations utilizing parsim, batchsim and Simulink Compiler

I anticipate that extra options to be launched in future variations of Simulink may even be primarily based on use of the Simulation Enter object, so I encourage you to begin utilizing it in the present day.

In its easiest kind, utilizing a Simulation Enter object provides just one line of code to the earlier strategy:

in = Simulink.SimulationInput(mdl)

You’ll be able to then move this to the sim command:

The primary benefit of the Simulation Enter object is that it explicitly specifies what’s being handed to the sim perform. For instance, let’s modify the beforehand created object by specifying:

in = in.setModelParameter(‘StopTime’,’20’);

  • A unique worth for the variable okay:

in = in.setVariable(‘okay’,20);

  • A sign for the root-level Inport block:

ds = createInputDataset(mdl);

ds{1} = timeseries([0 0 1 1],[0 10 10 20]);

in = in.setExternalInput(ds);

  • The preliminary states of the Integrator block:

x0 = Simulink.BlockDiagram.getInitialState(mdl);

x0{1}.Values.Information(2) = 0.1;

in = in.setInitialState(x0);

I can then simulate the mannequin and plot the outcomes:


Simulation object

In R2024a, we launched the Simulation object. This object permits you to programmatically begin, pause, step and tune block parameters in the identical manner as you’d do whereas interacting with the Simulink editor.

Right here is an instance that runs the simulation between 0 and 5 seconds with variable c set to 0, after which runs it between 5 and 10 seconds with c equal to 10:


Outdated and Discouraged

At this level, I must cowl approaches that we need to discourage. We’re offering these for backward compatibility, however we discourage utilizing them as a result of they arrive with many limitations and usefulness points.

Should you see a type of getting used, I strongly encourage you to transform your code to make use of the Simulation Enter object as described above.

Programmatically, this feature will be disabled utilizing:


As soon as that is executed, you may name the sim command with out output arguments:

As soon as the simulation full, you will notice the person parameters specified within the Information Import/Export part of the configset magically seem within the MATLAB base workspace.


One other even older syntax that also exists just for backward incompatibility is that this one, the place the sim command returns 3 outputs for time, states, and outputs:

As soon as once more, I like to recommend avoiding these and use the Simulation Enter object as an alternative.

Different Issues

Operating A number of Simulations

In case you are planning to run a number of simulations, I like to recommend creating an array of Simulation Enter object and passing it to the sim command. Let’s run our instance for a number of damping values:

% Setup an array of SimulationInput objects

in(1:5) = Simulink.SimulationInput(mdl);

in(i) = in(i).setVariable(‘c’,i);

out = sim(in,‘ShowProgress’,‘off’);



To run this code in parallel, all it’s essential do is substitute the decision to sim with parsim or batchsim.

Finest Apply for Information Administration

Simulations will be launched from a number of contexts: MATLAB Scripts, MATLAB Capabilities, MATLAB Lessons, App Designer apps, and so on. In your mannequin to be simply run from any context, I like to recommend associating together with your mannequin a set of default values for all of the parameters. That is usually executed in two methods:

When instances come to simulate the mannequin, you may create Simulation Enter objects to specify the info that should change from the default values related to the mannequin as illustrated above.

Now it is your Flip

Have you ever adopted the Simulink.Simulation object? What’s your most well-liked option to run simulations from MATLAB? Which challenges are you encountering when simulating fashions kind MATLAB?

Tell us within the feedback beneath.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments