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?
John Williams answered .
2025-11-20
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','[]')