Run a Simulink modell stepwise inside Matlab

10 次查看(过去 30 天)
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:
load_system("simmodel");
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;
end
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.

回答(1 个)

Abdolkarim Mohammadi
编辑:Abdolkarim Mohammadi 2020-9-24
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.
  3 个评论
Abdolkarim Mohammadi
I meant calling the MATLAB function out of the model like this:
Simon Meyer
Simon Meyer 2020-9-24
编辑:Simon Meyer 2020-9-28
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dependency Analysis 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by