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:
- Setting inputs
- Running the model for 1 step
- Getting the output data
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");
simIn = Simulink.SimulationInput('simmodel');
simIn = simIn.setModelParameter('SaveCompleteFinalSimState','on', 'SaveFinalState','on','FinalStateName','savePoint');
n = 0;
while n < 100
input = timeseries(0, n*str2double(step_size));
simIn = simIn.setModelParameter('StopTime',num2str((n+1)*str2double(step_size)));
simOut = sim(simIn);
myOperPoint = simOut.get('savePoint');
simIn.InitialState = myOperPoint;
value = simOut.simout.Data(2)
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.