Run one step of a simulink model, switch to an m-file, calculate, switch back to the model, run model with new calculated data. (Step wise Simulink execution)

17 次查看(过去 30 天)
Hi there,
I have a Simulink model which is generating Data. This Data is the input for some calculation in Matlab. Not just a function, there is a lot of stuff (object oriented) done. The result of the calculation should be the input for the next step of the Simulink model. Now the simulation simulates one time stamp and sends the result to Matlab. Matlab does his calculation and sends the result back to Simulink, and so on.
My problem is, how to synchronize Matlab and Simulink? With "sim()" I can just run all the simulation and do some post processing after everything is finished. But I want to change some parameters during the simulation which will take effect to the results.
I found the "sldebug()" mode witch is doing the step wise execution of the model. But it enters a special debug mode where I can not execute some Matlab code.
Thanks a lot for your help! Kilian

回答(3 个)

Kilian Lenz
Kilian Lenz 2016-2-11
After some time I found a solution. First you start Simulink from Matlab:
set_param('Test_Simulink_Model','SimulationCommand','start')
Then you can pause the simulation after a step of time with a pulse generator and a Matlab function block.
Inside of the block is writen
function fcn(u)
coder.extrinsic('bdroot')
coder.extrinsic('set_param')
if u > 0
set_param(bdroot,'SimulationCommand','pause')
end
end
In the Matlab skript you can now ask if the simulation is paused.
if(strcmp(get_param('Test_Simulink_Model','SimulationStatus'), 'paused'))
Now you are able to read the values from the output blocks. For that you have to get the data from the 'RuntimeObject' of the block.
%Get all Vehicle variables
rto_s_x = get_param('Test_Simulink_Model/Read CM Dict','RuntimeObject');
s_x = rto_s_x.OutputPort(1).Data;
To wirte back the data you just calculated use
set_param('Test_Simulink_Model/Gas','Value',int2str(gas));
and to continue the simulation use
set_param('Test_Simulink_Model','SimulationCommand','continue')
To give Simulink a littel bit of time to start again you have to pause the Matlabloop for a shortening
pause(0.01)
If everything is done you can stop Simulink with
set_param('Test_Simulink_Model','SimulationCommand','stop')
I hope this will help the next one doing the same.
Regards, Kilian
  1 个评论
Nitin Babu
Nitin Babu 2023-3-16
In the pulse generator what should the data be? I need my system to run for 0.1s, pause, do some calcs in matlab, feed that back into simulink mode, and run for the next 0.1s and repeat?

请先登录,再进行评论。


Vaibhav Awale
Vaibhav Awale 2016-1-13
You can run the simulation for one time step using the sim() command. The resulting outputs can be again sent as an input for simulating the model again using sim() command. If you wish to change simulation parameters in between the runs, you can use "set_param" function to set the desired parameters from command window or MATLAB script.
I hope this helps.
Regards,
Vaibhav

krishna teja
krishna teja 2020-3-28

类别

Help CenterFile Exchange 中查找有关 Naming Conventions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by