Run a Simulink modell stepwise inside Matlab

조회 수: 2 (최근 30일)
Simon Meyer
Simon Meyer 2020년 9월 24일
편집: Simon Meyer 2020년 9월 28일
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일
편집: 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
Abdolkarim Mohammadi 2020년 9월 24일
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에서 Programmatic Model Editing에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by