why evalin('caller', 'evalin(''caller'', ''a'')') works in a stack of script. but not a stack of functions?

67 ビュー (過去 30 日間)
raym
raym 2018 年 10 月 3 日
コメント済み: Stephen23 2018 年 10 月 7 日
Suppose three m files of script:
test1.m
a=1;
test2;
test2.m
b=2;
test3;
bb
test3.m
c=3;
evalin('caller', 'evalin(''caller'', ''a'')')
assignin('caller','bb','bbbhahaha');
Then test1 runs OK and shows the value of a;
However, if these scripts are headed with "function *" then error appeared:
Error using evalin Undefined function or variable 'a'.
Error in test3 (line 3) evalin('caller','evalin(''caller'',''a'')');
Error in test2 (line 3) test3;
Error in test1 (line 3) test2;
  14 件のコメント
dpb
dpb 2018 年 10 月 5 日
編集済み: dpb 2018 年 10 月 5 日
Those are not nested functions; that is a function called recursively.
As noted initially, this would require some refactoring of the organization; there is no documented/supported way to extend the reach as you're trying to do.
Stephen23
Stephen23 2018 年 10 月 6 日
編集済み: Stephen23 2018 年 10 月 6 日
@raym: you might like to read this:
It explains what anonymous, local, and nested functions are, with links for more details. Reading the documentation is a good way to learn how MATLAB works, and what terms mean.
"I tested it and found that it is not true:"
Actually nested functions certainly can access their "parent" function's workspace, just as dpb stated: I use them all the time for this very convenient feature. Your example code does not use any nested function. Note also that any type of function call itself recursively (like in your example code).

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

採用された回答

Jan
Jan 2018 年 10 月 6 日
編集済み: Jan 2018 年 10 月 6 日
While using functions is surely the best answer, the actual question concerned scripts. Then you do not have to call evalin at all but you can access the variables directly. This is the only benefit of scripts:
% file: test1.m
a=1;
test2;
% file: test2.m
b=2;
test3;
disp(bb)
% file: test3.m
c=3;
bb = 'bbbhahaha';
disp(a)
Works. So simply omit the evalin call. Or restructure the code to use functions and inputs/outputs to get a clean and efficient code.
  2 件のコメント
dpb
dpb 2018 年 10 月 6 日
Yeah, if he's always working at the level of the script but my reading is that he's calling existing functions starting from the script in logic-dependent ways such that the variable to which wants access is not always at the same location in the call stack of the caller but in this case specifically a level higher. To get to that variable that is local inside that function isn't doable by scripting alone, either, without turning those functions into scripts as well and that undoubtedly will lead to scoping issues.
I see no way without refactoring at least some...
Stephen23
Stephen23 2018 年 10 月 7 日
"Or restructure the code to use functions and inputs/outputs to get a clean and efficient code."
Agreed. I still don't see why basic input/output arguments can't be used. I use them all the time for parsing trees using recursive functions in order to extract particular data at particular levels of the tree, and I don't see why a few function calls passing data as inputs/outputs would not be possible here. Playing with the stack is hack code that won't be neat or efficient...

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCode Execution についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by