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:

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
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)
plot(out.yout.get(‘dx’).Values)
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
- 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:
plot(out.yout.get(‘dx’).Values)
Simulation object
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:
plot(out.yout.get(‘dx’).Values);
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:
set_param(mdl,‘ReturnWorkspaceOutputs’,‘off’);
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.
logsout
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’);
plot(out(i).yout.get(‘dx’).Values);
finish
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.