Set simulation time and fixed step size for a Simulink model from the command line

650 views (last 30 days)
Hi! I have a simulink model that I need to simulate over a few different durations and with a fixed (but different) sampling time. So what I want to do is to set this from the command line without having to start up simulink and change it manually each time.
To clarify I want to set the simulation duration (or the start time/stop time) and the solver options to Fixed-step (or at least change the step-size). So it would look something like this in a script/the command line:
sim('simModel', 'simulationTime', [0 10], 'solverOptions.stepSize', 1/1024)
Where 'simModel' is the simulink model in question.
It doesn't have to look like this, but just to give you an idea of what I want to do. Is there a way to do this?
I can't really find it in the documentations, the only thing I find is how to set the sampling time for a specific block, but I want to keep those as inherited.

Accepted Answer

Sebastian Castro
Sebastian Castro on 12 Nov 2015
If you open the model's Configuration Parameters, for each parameter you can right-click and select "What's This?". This will open up some documentation, which includes a table that shows you how to programmatically use that parameter.
So, you can take that and use it in the sim command as you specified:
>> sim('modelName','StartTime','0','StopTime','10','FixedStep','0.2');
Notice that the properties have to take a string value and not a numeric value. This is where the num2str function comes in handy. For example,
for Ts = 0.01:0.01:0.1
simout = sim('modelName','FixedStep',num2str(Ts));
end
- Sebastian
  5 Comments
mehdi azari
mehdi azari on 25 Feb 2021
I want to change the simulation time from seconds to minutes and hours. How should I do this?
Walter Roberson
Walter Roberson on 23 Apr 2022
The stop time always have to be passed to simulink in seconds; https://www.mathworks.com/help/simulink/gui/stop-time.html
If you have hours and minutes then
st_secs = seconds( hours(NumberOfHours) + minutes(NumberOfMinutes) );
num2str(st_secs)
or
st_secs = seconds( duration(NumberofHours, NumberOfMinutes, 0) )
num2str(st_secs)

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!