persistent variables are not empty

24 次查看(过去 30 天)
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?

采纳的回答

Geoff Hayes
Geoff Hayes 2014-8-7
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!
  2 个评论
K0ertis
K0ertis 2014-8-7
Sorry, my bad! It's indeed how you've described.
Thank you very much!
Image Analyst
Image Analyst 2014-8-7
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by