How does clearing nested function workspace works?
Afficher commentaires plus anciens
I found that this code works :
function main
message = 'hello world!';
figure;
uicontrol('style','pushbutton',...
'string','show message',...
'callback',@(hObject,eventdata) cb);
function cb
disp(message)
end
end
Function cb is nested, and Variable message is shared in cb.
In my expectation, after main is executed and reach end, the function workspace of main will be cleared and the variable message is no longer accessible from any function. However if I press the button, it executes callback cb and show "hello world!". Even after I called clear all in base workspace (command window), it still runs correctly. How does this happen? What point am I missing?
EDIT :
This code also works :
function main
message = 'parent function workspace is accessible';
figure;
uicontrol('style','pushbutton',...
'string','show message',...
'callback',@(hObject,eventdata) cb);
function cb
disp(message)
another_nestedfunction
another_localfunction
end
function another_nestedfunction
disp('nested function is accessible')
end
end
function another_localfunction
disp('local function is accessible')
end
All of nested function, local function, parent function workspace are accessable.
This is even more strange: I executed this file, and removed this file from search path, but the callback cb still works! Furthermore, I changed the messages in each function's disp in this script, and it 'knows' the local/nested function has been changed and shows changed messages, even this file is not on search directory! Is there any hidden mechanism on referring function code? How does this happen?
Réponse acceptée
Plus de réponses (1)
Hyeokjin Jho
le 22 Avr 2021
0 votes
Catégories
En savoir plus sur Environment and Settings dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!