How to NOT share variable with nested function?

5 ビュー (過去 30 日間)
Thomas Fournier
Thomas Fournier 2021 年 8 月 11 日
編集済み: Stephen23 2021 年 8 月 12 日
Hello,
I made a big code in a separate m.file, this code includes functions. I then want to make a function out of this big code, but, by doig so, i create nested functions and, as those function share the sames variable names, nothing is working properly anymore.
Is there a way to precise that the nested function inside a function should not share variables with the main function and with each other?
Thank you for your help!
  2 件のコメント
Stephen23
Stephen23 2021 年 8 月 12 日
編集済み: Stephen23 2021 年 8 月 12 日
@Thomas Fournier: By definition, the scope of nested functions includes all variables from their parent function/s:
If you need to use nested functions, then you will need to rename those variables.
But I suspect (due to that the code originally worked "in a separate m.file") that you do not need nested functions at all, and you can probably simply define them as local functions together with one main function at the start of the file, thus trivially avoiding the whole problem:
Thomas Fournier
Thomas Fournier 2021 年 8 月 12 日
Thanks a lot it does work!

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

採用された回答

Walter Roberson
Walter Roberson 2021 年 8 月 12 日
You took a script that was of the form
some code
function first
%something
end
function second
something
end
and you converted it to a function by putting a function line at the beginning and an end at the end of it, like
function do_stuff
some code
function first
%something
end
function second
something
end
end
What you should have done instead is
function do_stuff
some code
end %end HERE, not at bottom
function first
%something
end
function second
something
end
  1 件のコメント
Thomas Fournier
Thomas Fournier 2021 年 8 月 12 日
It works perfectly thank you!

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

その他の回答 (2 件)

darova
darova 2021 年 8 月 11 日
編集済み: darova 2021 年 8 月 11 日
If you declare function or variable inside nested function it rewrites itself. There is no option for mistake
function main
a = 2;
b = 3;
a + b
function nest1
a = 5;
a + b
end
a + b
end
  4 件のコメント
Walter Roberson
Walter Roberson 2021 年 8 月 12 日
The line
a = 5;
does not make a into a local variable inside the nested function: instead it writes to the shared variable.
Stephen23
Stephen23 2021 年 8 月 12 日
編集済み: Stephen23 2021 年 8 月 12 日
"But why a value in main is changed?"
Shared variables are explained in the MATLAB documentation:

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


Jeff Miller
Jeff Miller 2021 年 8 月 11 日
It sounds like your "big code" is a script rather than a function, but now you want to make a function out of it.
I suggest you remove the functions from the "big code" script and put them in separate files. Then make "big code" into a function, but these other functions will no longer be nested so each will have its own separate variables.

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by