フィルターのクリア

Extend scope of variables to other local functions

4 ビュー (過去 30 日間)
Jurgen
Jurgen 2016 年 8 月 26 日
編集済み: Stephen23 2016 年 8 月 26 日
I have a function that uses a few helper functions. I'd like these helper functions to have access to variables without:
  • passing variables
  • making nested functions
  • making globals
E.g. when I call a local function from another function in the same m-file, the body of the called function is evaluated in the scope/namespace of the caller. As far as I know `evalin()` only works with single expressions.
  5 件のコメント
Adam
Adam 2016 年 8 月 26 日
I do certainly miss namespaces from C++
Guillaume
Guillaume 2016 年 8 月 26 日
In C++, unless your variables are extern static (i.e. matlab's global) you won't be able to share variables between functions in different files even at the namespace scope.

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

回答 (1 件)

Guillaume
Guillaume 2016 年 8 月 26 日
my m-files are sets of related functions, almost like classes
Then use classes, you can have your shared variables as private properties of the class, and your functions as methods of the class.

カテゴリ

Help Center および File ExchangeScope Variables and Generate Names についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by