Run a Simulink modell stepwise inside Matlab

1 view (last 30 days)
Simon Meyer
Simon Meyer on 24 Sep 2020
Edited: Simon Meyer on 28 Sep 2020
Hello together!
I'm currently trying to integrate a Simulink model into a Matlab function and simulating it step-wise.
Goal would later be to integrate a (already existent) huge model with a vehicle motion control into a simulation run by Matlab.
This simulation needs the model to run step-wise for adjusting input variables and getting the results every step.
So the plan would be:
  1. Setting inputs
  2. Running the model for 1 step
  3. Getting the output data
  4. Repeat.
The internal states of the simulation should of course still remain.
I searched quite some time and have now done the following approach:
step_size = get_param("simmodel", "FixedStep");
% generate a SimulationInput object from the model
simIn = Simulink.SimulationInput('simmodel');
% change general settings to allow saving it's state
simIn = simIn.setModelParameter('SaveCompleteFinalSimState','on', 'SaveFinalState','on','FinalStateName','savePoint');
n = 0;
while n < 100
% here set input parameter for model
input = timeseries(0, n*str2double(step_size));
% set the stop time to the current time + 1 step_size
simIn = simIn.setModelParameter('StopTime',num2str((n+1)*str2double(step_size)));
% simulate with the settings
simOut = sim(simIn);
% get the current state
myOperPoint = simOut.get('savePoint');
% apply this state to the SimulationInput object
simIn.InitialState = myOperPoint;
% here example of getting output signals from model
value = simOut.simout.Data(2)
n= n+1;
So the idea is to simulate the model while saving its final states, setting these states as the inital states in the SimulatinInput object and simulating the next step.
First, is that approach working for my purpose? For my small test model it seems fine, but could I run in to problems with the big model?
But my main question:
Is there any better approach regarding runtime? Currently simulating one step takes approx. the same time than simulating the whole at once.
I hope the problem / my question is clear.
I would be happy if there is anyone out there who has experience in this area and can provide input and source code if possible.

Answers (1)

Abdolkarim Mohammadi
Abdolkarim Mohammadi on 24 Sep 2020
Edited: Abdolkarim Mohammadi on 24 Sep 2020
You can use MATLAB function block. It is executed on every timestep just like other blocks, and you can return outputs for every step to the MATLAB workspace at the end of the simulation for further processing by connecting it to a To Workspace block. I think it is simpler than your current approach.
Simon Meyer
Simon Meyer on 24 Sep 2020
Edited: Simon Meyer on 28 Sep 2020
Yeah, I did understand what you meant.
But like I said, the whole project will be run by our simulation. In this simulation a Matlab function is called where we can do what we need to do (like calling other functions, MEX functions, if possible the Simulink model, etc.). The simulation is the source for our "trigger", our step.
So there is no way in calling the Matlab function out of the simulink model.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!