How do I froce MATLAB to wait for Simulink simulation to be paused or stopped before resuming ?

28 次查看(过去 30 天)
Hello,
I am currently facing a problem with MATLAB and Simulink. I have a MATLAB script that calls and opens a Simulink model.
I start my simulation using the command :
set_param(mdl,'SimulationCommand','start');
Then I enter a "for" loop that will pause the Simulink simulation, process the data , update the next time for pause and resume the simulation.
In my Simulink model, I have an assertion that will pause the simulation when a certain simulation time is reached as explained on that page :
with the callback :
set_param(mdl,'SimulationCommand','pause')
Once the simulation is paused I get the variables that are in a "To Workspace" block in my Simulink model, process them with another script or software and re-inject them as an input for my model. Then I update my assertion to pause at the next time step and finally I resume my simulation using the command :
set_param(mdl,'SimulationCommand','continue');
and go back to the first line of my loop.
The problem is :
It seems, that when I "continue" my simulation, that MATLAB doesn't wait for my simulation to be paused again before continuing to play the script. This is the reason why I have artificially added the following lines to be sure that my simulation is paused when i try to read data from it in the workspace :
while(~strcmp( get_param(mdl,'SimulationStatus') , 'paused'))
pause(0.01);
end
since the pause() function in MATLAB allows for Simulink simulation to keep running, the method actually works, but with a major issue :
The pause() function is limited to a minimum of 0.01 seconds, and this is way too long. To solve that problem i have been looking for codes that would pause MATLAB for a shorter amount of time (typically 1 millisecond would be very satisfactory for the application that I am running) but all the solutions I have found online, even if they manage quite accurately to stop MATLAB's execution for the desired amount of time, they also have the major inconvenience to stop Simulink's execution too ! That means that the "while" loop i use to wait for Simulink to compute and pause becomes actually an infinite loop.
That is why I am calling for help to the community today, if there is a better way to do the actions that i want to realise.
PS : Before someone suggests me to use the "Matlab function" block in Simulink, I tried it too but this method is not suitable for the application tha I am running.
Thanks in advance for your help,
Regards,
Philippe

回答(1 个)

Fangjun Jiang
Fangjun Jiang 2016-3-17
You are trying to constantly monitor the "SimulationStatus" of your model. That is not efficient.
I wonder, in the callback of your assertion block, after pausing the simulation, can you add another function call to process the data, re-set the parameter of the assertion block and resume the simulation? You can always add a flag in that function or check some conditions to decide when you want to stop the simulation thus finish the whole task.

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by