persistent variables are not empty

18 views (last 30 days)
Hi, I often used persitend in the past, as I remember it always returned an empty variable. I've made a screenshot after I started the Simulink model the first time and it doesn't return an empty matrix (every time, as often I restart the simulation). Sometimes it starts pNew as NaN, quiet strange...
May you help me?

Accepted Answer

Geoff Hayes
Geoff Hayes on 7 Aug 2014
K0ertis - when you restart the simulation, are you clearing the function so that all persistent variables are reset? According to persistent
persistent X Y Z defines X, Y, and Z as variables that are local to the function in which they are declared; yet their values are retained in memory between calls to the function.
So if you run the first simulation - after a restart of MATLAB - then pNew will be empty. And as the simulation progresses, calls to this function will use the value of pNew from previous calls. Now if you stop the simulation, and start the next one, won't the airbag function still be in memory with the same persistent values set as before (during their last call)?
Again from the above link,
Whenever you clear or modify a function that is in memory, MATLAB also clears all persistent variables declared by that function.
So I think that you want to do is to clear these persistent variables by adding some sort reset/refresh command to the beginning of your simulation which just clears any and all persistent variables from across all of your functions. Something like
clear functions
Try the above and see what happens!
K0ertis on 7 Aug 2014
Sorry, my bad! It's indeed how you've described.
Thank you very much!
Image Analyst
Image Analyst on 7 Aug 2014
Just to be clear you don't want "clear functions" inside your script or function otherwise it will blow it away. You want to execute that at the command line. You can make a shortcut on the toolbar for it to make it real convenient!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!