How to Access a Variable's Value in a Nested Function?

7 ビュー (過去 30 日間)
Rightia Rollmann
Rightia Rollmann 2017 年 2 月 28 日
コメント済み: Adam 2017 年 2 月 28 日
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
  1 件のコメント
Adam
Adam 2017 年 2 月 28 日
The shared workspace of a nested function and the main function is not bi-directional. The nested function has access to everything in the workspace of the main function, but not the other way around.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2017 年 2 月 28 日
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.

その他の回答 (0 件)

カテゴリ

Find more on Workspace Variables and MAT-Files in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by