MATLAB: How to freeze a Simulation in Simulink, close MATLAB, and run the simulation again but starting from that same saved state

operatingpointsimulink

I would like to run a simulation in Simulink following the below steps:
1. Run the simulation from time 0 to 10s
2. Stop the simulation at time 10s and save all the states and operating points of the simulation at that timestep
3. Close MATLAB
4. Re-open MATLAB/Simulink and then re-run that same model starting from time 10s to 20s and starting from the operating point and states I saved at time 10s. I do not want the model to restart the simulation from time 0 and lose the final simulation state of step 1.
How can the above workflow be achieved? 

Best Answer

This is an ideal application for a "ModelOperatingPoint" object. The "ModelOperatingPoint" object is a MATLAB object that stores information about the final state of the simulation at a given stop time. For this use case, that stop time is 10 seconds. You can then use the "ModelOperatingPoint" object to run the same model again but this time starting from the time-step and simulation state that was saved at 10 seconds till any new stop time you specify without losing the data that was generated during the initial run.
To get started on "ModelOperatingPoint", please refer to the following documentation link:
https://www.mathworks.com/help/releases/R2020b/simulink/ug/saving-and-restoring-simulation-operating-point.html
Below are two scripts, executed in sequence, that should execute the above required workflow for the attached example Simulink model called "vdpOperatingPointExample.slx". The attached model "vdpOperatingPointExample.slx" is a copy of one of the Simulink shipped examples called "vdp" that models the van der Pol equation. More resources on how to programmatically control and simulate the "vdp" shipped example can be found here:
https://www.mathworks.com/help/releases/R2020b/simulink/ug/using-the-sim-command.html
To demonstrate the use of model operating points for saving a model's operating point and re-starting the simulation from that point, first, download the attached model: "vdpOperatingPointExample.slx".
Then, in the same MATLAB working directory as the downloaded Simulink model, please execute the below script:
 
open_system('vdpOperatingPointExample') %open the Simulink model
%make sure the initial state of the model is set to default empty
set_param('vdpOperatingPointExample','InitialState','[]')
%enable the option of saving the last simulation operating point
set_param('vdpOperatingPointExample','SaveFinalState','on','FinalStateName',...
'myOperPoint','SaveOperatingPoint','on');
%run the simulation for 10 seconds
% because the last state at time equal to 10 seconds will be saved
simOut = sim('vdpOperatingPointExample','StopTime','10')
% when a Simulink model completes its simulation
% it will save the output in a structure by default called "simOut".
% Below, the operating point called "myOperPoint" is being extracted
% and saved locally in the MATLAB base workspace
myOperPoint = simOut.myOperPoint
% Save the operating point that was extracted (the one at time 10)
% inside a MAT-file called "data.mat" to be able to close MATLAB
% and not lose the data
save('data','myOperPoint')
save_system('vdpOperatingPointExample') %save changes to model
Now you can close MATLAB and then execute the below script to extract the saved operating point and then start the simulation from time 10s:
open_system('vdpOperatingPointExample')
%{
The below command is optional. It was added it just to point
out that you probably would want to turn off the option
of saving the final operating point because we don't need
it anymore and we don't want it to interfere with
the original operating point
%}
% disable the option of saving the last operating point
set_param('vdpOperatingPointExample','SaveFinalState','off',...
'SaveOperatingPoint','off');
%%%%%%%%%%%%%%%
% The below is mandatory
% load the operating point we saved in the data.mat MAT-file
load data
% set the initial state of the system to be the operating point
% last saved in the last simulation
set_param('vdpOperatingPointExample','LoadInitialState','on','InitialState',...
'myOperPoint');
% execute the simulation for stop time 20 seconds now
% it should start from t = 10s
sim('vdpOperatingPointExample','StopTime','20')
%reset initial state to default empty when sim is over
set_param('vdpOperatingPointExample','InitialState','[]')