Persistent variables don't persist outside an executed callback function
16 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi everyone guys, i would submit a question that's maybe can be trivial but i never encountered this problem before: I'm programming a programmatic GUI, in this GUI i have used a persistent variable that's will reassigned in many step inside a nested function in a callback
function main_GUI()
persistent V
V = [] ; % initialize the variable
function callback(...)
V = test (...,V)
end
end
___________________
function V = test(...,V)
V = 0 ;
...some commands
V = 1 ;
...some commands
V = 'exit'
end
my problem is that the variable V after exit the function callback seems to be cleared and don't persist the value V = 'exit'!? have someone an idea? many thanks in advance :-) Luca
1 Commento
karandeep singh seehra
il 8 Ago 2024
One thing you should consider is that persistent variables are only accessible in different function, but can only be changed in the function it is defined
Risposte (1)
Geoff Hayes
il 15 Mag 2016
Luca - why are you declaring the variable V as persistent? You would only do this if you want to retain the value of this variable between function calls. As your main_GUI will only be called once, declaring V as persistent is unnecessary.
So at what point does V get cleared? It will always be empty (it's initial value) until the callback is called. Just because your function is nested within main_GUI, this doesn't mean that it will be called in the order of
- Initialize V to empty matrix
- declare function callback with body
- etc.
All you are doing in the above code is declaring the nested callback function. It won't get evaluated until it is called by whatever control is using this callback.
Also, there is no need to pass V into the callback function. Since V is a local variable in the main_GUI workspace, and callback is a nested function within this workspace, then the function has access to the variable (and any other) by default.
function main_GUI()
V = [];
function nestedFunction()
if isempty(V)
V = 42;
else
V = V + 1;
end
end
nestedFunction;
fprintf('V is %d\n', V);
end
It isn't until nestedFunction is called that V is assigned a value.
3 Commenti
Geoff Hayes
il 17 Mag 2016
Luca - please re-read the documentation for persistent. Declaring a variable as persistent is not necessary for nested variables. You would only declare it as persistent if you wish the variable to retain its value between repeated function calls. But as your main_GUI or TESTPR are only called once, declaring as such would have no effect.
As for why psv isn't being updated, look closely at your code.
function Start_Btn_Callback(hObject,handle)
psv = testfunction(psv) ;
end
psv, the local variable defined in TESTPR is only updated when the testfunction completes.
function psv = testfunction(psv)
psv = 2 ;
pause(3)
psv = 3;
pause(3)
psv = 7;
end
The psv that is updated in the above function is a local variable to testfunction. It isn't the psv defined in TESTPR. And so that psv will only change after six seconds.
What I might do is change your two functions to
function Start_Btn_Callback(hObject,handle)
testfunction;
end
function testfunction
psv = 2 ;
pause(3)
psv = 3;
pause(3)
psv = 7;
end
The above assumes that both functions are nested in TESTPR. If testfunction is not nested, then you will have to come up with an alternative means to update psv.
Vedere anche
Categorie
Scopri di più su Environment and Settings in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!