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.).
Best Answer