MATLAB: SimState error

inverted pendulumSimEventssimstatesimulink

Hello, i'm working with Simulink and I have a problem: I start a simulation and every 'T' time I need to stop it, save the state of the system as Simstate, and then restore the system state by changing a few variables. For example, I am working with the base model 'Inverted Pendulum with Animation' (penddemo.mdl), and when I restore the state I must change the mass of the pendulum, here's some code:
set_param('penddemo','LoadInitialState','on','InitialState','init_state'); % load the initial state
set_param('penddemo','SaveFinalState','on','FinalStateName', 'xFinal','SaveCompleteFinalSimState', 'on'); % save the complete simstate
set_param('penddemo/Pendulum','Mcart','0.600'); % change the mass
sim('penddemo',time); %starting the simulation
The problem is that I get this error:
can not load the Simulink SimState Because The initial model, 'penddemo', was changed after the SimState was saved. Run the simulation again and resave the SimState.
I know that there are limitations in the use of Simstate:
You cannot make any structural changes to the model between the time at which you save the SimState and the time at which you restore the simulation using the SimState. For example, you cannot add or remove a block after saving the SimState without repeating the simulation and saving the new SimState.
but I do not think changing a single variable is a structural change and Mcart is a tunable parameter. Maybe someone knows another way to change some variables before the recovery of Simstate.

Best Answer

The parameter 'Mcart' on the block 'penddemo/Pendulum' is used under the mask in a Fcn block in the "Expression" parameter, which is non-tunable (ie. cannot change between a SimState save and restore). So you cannot change the value of 'Mcart' because it counts as a structural change.