MATLAB: How to Access a Variable’s Value in a Nested Function

nested function

How Can I see the value of variable g in Command window and Workspace?
function parentfcn(b)
a = b+1
function childfcn
g = a+1
end
end

Best Answer

The nested workspace stops existing as soon as parentfcn returns.
See however,
function r = parentfcn(b)
a = b+1
childfcn();
r = @childfcn;
function childfcn
g = a+1
end
end
together with
bbb = parentfcn(7)
s = functions(bbb)
s.workspace{1}
Notice that even though you called childfcn, that the workspace of childfcn does not include the local variable g: the workspace retrieved only includes the objects that the child shares with the parent. g stops existing as soon as childfcn returns.