"Cannot find state N in the given variable." Error when trying to start a simulation using previously saved states.

1 view (last 30 days)
I am using Simulink to simulate a large model, that includes S functions.
I am using R2018a and I am saving the states at the end of a simulation using the Dataset format.
R2018a does not support 'SaveOperatingPoint' so I am just saving the states, not the whole operating point.
When I try to run the next simulation, by loading the states (using set_param(model, 'LoadInitialState', 'on'); before the run), I am getting the following error which I am not sure how to address:
Cannot find state 339 in the given variable.
Component:Simulink | Category:Model error
How can I find out which block, or part of the model, is causing this?
I have some S functions, in which I have enabled the mdlGetSimState() and mdlSetSimState() methods, which seem to be working correctly.
I have also included the relevant calls to:
ssSetSimStateCompliance(S, USE_CUSTOM_SIM_STATE);
ssSetSimStateVisibility(S, 1U);
which seem to compile correctly.
I have the option to try again with MATLAB/Simulink R2021b, which I can try tomorrow. The mdlGetSimState() and mdlSetSimState() methods will then become mdlGetOperatingPoint() & mdlSetOperatingPoint(), and ssSetSimStateVisibility(S, 1U); will be replaced by ssSetOperatingPointVisibility (S, 1U);. I can then also TRY with 'SaveOperatingPoint' set ON as well. But, I am not feeling confident that this will solve the problem. I suspect it will remain.
How can I locate the source of the "Cannot find state 339 in the given variable." error?
  6 Comments
Andrew Roscoe
Andrew Roscoe on 10 Jun 2022
This model includes heirarchical library links, and variant subsystems.
I have established, using a much smaller test model, that it is possible to change the parameters at run-time, even within the masks of subsystems within linked library blocks (parameterised link changes), WITHOUT causing any problems for the 2nd simulation (using the saved states). I can also do this within library-linked blocks that contain variant subsystems (themselves containing library-linked blocks), without any trouble.
I have also established that you can change the variant subsystem choices, after the 1st run completes and states are saved, so long as you change them BACK again to the original variant selections, before starting the 2nd run that loads the initial states. This means that simply "touching" the model does not cause a problem, so long as it ends up back at the original state.
But, if I genuinely change any variant subsystems between the 1st and 2nd runs, in the small test model, then I get the problem error message:
"Simulink cannot load the initial SimState because the model, 'xxxx', was changed after the SimState was saved. Run the simulation again and resave the SimState."
This makes perfect sense. So, this is my main line of investigation now. I have to determine if any variant selections are being made differently before the 2nd run. This sounds trivial, but it is not, due to the size of the main model itself, and the size of the historical MATLAB script infrastructure surrounding the existing model. It would be very helpful if that error message could give me some pointer to the block path(s) that it believes have been changed.
Fangjun Jiang
Fangjun Jiang on 10 Jun 2022
If only small number of states need to be initilized and majority of the states take default or zero initial value, then using State Read/Write blocks might be easier.

Sign in to comment.

Answers (1)

Fangjun Jiang
Fangjun Jiang on 9 Jun 2022
set_param(model, 'LoadInitialState', 'on') just turns on that checkmark, you need to provide the variable for the initial state.
Do it manually first, at Configuration Parameters, Data Import/Export, turn on "Final States" "xFinal", run one simulation.
Next, specify the "Initial state" as "xFinal" and run simulation again.
In between, you can check if "xFinal" has all the states. It looks like you have a lot of states. Make sure all S-functions have their states properly defined.

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!