persistent variables are not empty
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
K0ertis
el 7 de Ag. de 2014
Comentada: Image Analyst
el 7 de Ag. de 2014
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/154140/image.png)
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?
0 comentarios
Respuesta aceptada
Geoff Hayes
el 7 de Ag. de 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!
2 comentarios
Image Analyst
el 7 de Ag. de 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!
Más respuestas (0)
Ver también
Categorías
Más información sobre Axis Labels en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!