Start Simulink sim with persistent variables, using stored state

I have a simulink simulation with matlab function blocks that contain persistent variables.
Since switching to Matlab R2022, (from R2021a) I have been unable to start my Simulink sim from a saved state and have have the persistent variables populated. Is there something particular that needs to be done to save the persistent variables? These revert to the null vector when starting from a saved state.

5 Commenti

Hi Leena,
Are you using the workflow for Model Operating Point to save the final state from the first sim and use that to intialize the second simulation?
Paul,
Thank you for your response to the workflow link. I think i have followed the workflow, however, I have done so using matlab to set the Simulink configuration parameters, instead of changing the simulink models per the steps laid out in the workflow. In effect, I am following the steps defined in the Save and Restore example section for Simulink.setInitialState. The whole idea is not to be manually modifying the Simulink model each time i wish to run it.
So, I set the "Save Final States" (and "save Operating Point") Config parameters from the above matlab call and not in the Simulink diagram. Likewise, I use setInitialState(simOut1.finalOP) per the above example, without turning "Load from Workspace/Initial State" on. Is this not acceptable?
Seemed alright until R2022. Hopefully I'm missing a new step that R2022 has introduced and not that we need to be modifying the simulink sim each time we wish to run things from now on!
I'll just note that the doc page I linked doesn't say anything explicitly about Matlab Function blocks or saving persistent variables in those blocks. But, if it worked in 2021a, no reason to think it shouldn't work in 2022a (or b), unless there was a documented behavior change (or maybe in 2021a it was an undocumented feature that could be removed at any time). Maybe the Release Notes from 2021a onward indicate a change to Model Operating Point behavior? If you can't find anything there, you can always contact Tech Support and see what they have to say about this.
Yes, i think i am going to have to try that route. thanks anyhow!
I should have suggested to also check the Bug reports as well.
If you do contact Tech Support, would you mind posting back here summarizing the response? Probably of interest to others using the Model Operating Point feature.

Accedi per commentare.

Risposte (1)

Suppose that you have followed these guides with the persistent variable initialization in your model: DOC

1 Commento

Yes, the initialisation does conform to the guidelines. However, to be clear, the problem doesn't occur when the Simulink sim is first run to save the states from T= T9 to T0+1, for instance. It occurs in a subsequent run when the sim rungs from T0+1 to T0+2.
I can see (in debug mode) that the persistent variables are initialised, populated etc during the initial run. In the next run when starting using:
that the persistent variable array that had been populated is now empty.
FYI, this same code base worked as written in Matlabs R2020b and R2021a. In upgrading to R2022b, the code doesn't perform as desired as what should be a populated array of variables with history has now reverted to zero.

Accedi per commentare.

Categorie

Prodotti

Release

R2022b

Richiesto:

il 15 Feb 2023

Commentato:

il 16 Feb 2023

Community Treasure Hunt

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

Start Hunting!

Translated by