How does clearing nested function workspace works?

5 visualizaciones (últimos 30 días)
Hyeokjin Jho
Hyeokjin Jho el 7 de Mayo de 2020
Respondida: Hyeokjin Jho el 22 de Abr. de 2021
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?

Respuesta aceptada

Stephen23
Stephen23 el 7 de Mayo de 2020
Editada: Stephen23 el 7 de Mayo de 2020
I have use nested functions extensively for many years, and have also been curious about this behavior. As far as I have found, the MATLAB documentation does not make any explicit comment about what happens in these kind of situations. The clear documentation does state that "Specifying a local or nested function is not supported" which perhaps hints at the internal behavior. Based purely on my observations using nested functions, their behavior seems to be something like this:
  • a workspace is created which is accessible to both the parent and the nested function.
  • for as long as the nested function can still be called (e.g. via callback, function handle, etc.), that workspace will continue to exist.
Lets call it a "snapshot" workspace.
According to this understanding, clearing the parent function does not really make a difference, because the nested function simply retains the entire snapshot workspace for as long as the nested function is callable. I have never needed to do extensive tests into exactly when the snapshot workspace stops existing (e.g. deleting all relevant graphics objects and/or function handles, reloading the function from file, calling the parent function again, etc.).
It is also worth mentioning that nested functions use a static workspace, i.e. variables cannot be added or removed dynamically: https://www.mathworks.com/help/matlab/matlab_prog/variables-in-nested-and-anonymous-functions.html

Más respuestas (1)

Hyeokjin Jho
Hyeokjin Jho el 22 de Abr. de 2021
While I study Javascript, I learned this behaviour is called closure
"A closure is a function having access to the parent scope, even after the parent function has closed."

Categorías

Más información sobre Environment and Settings 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!

Translated by