MATLAB: Simulink/simscape final state ‘cannot find a matching block state to element 135’ output by the same model

restartingSimscapeSimscape Electricalsimulink

I have a model that contains some simulink and simcape model.
The gist is: initail simulation from T0 to T1, perfrom some analysis, then restart at T1 and run to T2, etc. But this breaks when restarting T1. Kindly note: This intermediate analysis affects workspace parameters that affect values of e.g. electrical resistances in the model.
cset = getConfigSet( gcs, 'Configuration1' );
set_param(cset, 'Solver','VariableStepAuto')
set_param(cset, 'SolverName','VariableStepAuto')
set_param(cset, 'LoadInitialState','off');
set_param(cset, 'StateSaveName', 'state_history');
set_param(cset, 'SaveFinalState','on')
set_param(cset, 'FinalStateName','xFinal')
set_param(cset,'StartTime', string(T0) );
set_param(cset,'StopTime', string(T1) );
simOUT = sim( gcs, cset );
assignin('base','xFinal',simOUT.get('xFinal'))
set_param(cset, 'LoadInitialState','on')
set_param(cset,'InitialState', 'xFinal')
set_param(cset,'StartTime', string(T1) );
set_param(cset,'StopTime', string(T2) );
simOUT = sim( gcs, cset );
The first run is fine. everything looks fine. The second model call yields:
Unable to load the specified initial state for model 'your_model'. Cannot find a matching block state
corresponding to element 135 of signals structure array.
Nothing has changed in the model, and the error sound like as if a block has simply vanished.
This is field is present in the first run's output:
simOUT.xFinal.signals(135)
ans =
struct with fields:
values: 16.6848539456316
dimensions: 1
label: 'CSTATE'
blockName: 'your_model/comp1/C'
stateName: 'your_model.comp1.C.p.v'
inReferencedModel: 0
and is the exact same thing stored in 'xFinal' that's parsed to the model workspace and pushed as an initial state.
I'm wondering if restarting models with simscape elements need further considerations than those shown in : https://www.mathworks.com/help/simulink/slref/saving-and-restoring-simulation-operating-point.html

Best Answer

Are you trying to use operatining points? If you are:
Comparing your code to the examples in that link to the doc two things stand out ....
In your first call to sim do you need 'SaveOperatingPoint', 'on' ?
In your second call to sim, should you be setting the initial time to T1? The doc explicitly says:
"The start time value must remain 0.0 (that value was the start time of the original simulation). The software stores the original simulation start time in xFinal. This value must match the start time of the current simulation to enable restoration of the simulation state." So in your case maybe you should set the start time to T0 in the second call to sim.
I also came across this tip: Before you save the operating point, disable the Block Reduction parameter in Configuration Settings > Simulation Target > Advanced Parameters.