Is it possible to write several functions into a single m-file?

20 ビュー (過去 30 日間)
Mr M.
Mr M. 2015 年 5 月 20 日
コメント済み: Stephen23 2015 年 5 月 20 日
Is it possible to write several functions into a single m-file and use them locally inside that file?

採用された回答

Thomas Koelen
Thomas Koelen 2015 年 5 月 20 日
The first function in an m-file (i.e. the main function), is invoked when that m-file is called. It is not required that the main function have the same name as the m-file, but for clarity it should. When the function and file name differ, the file name must be used to call the main function.
All subsequent functions in the m-file, called local functions (or "subfunctions" in the older terminology), can only be called by the main function and other local functions in that m-file. Functions in other m-files can not call them.
In addition, you can also declare functions within other functions. These are called nested functions, and these can only be called from within the function they are nested. They can also have access to variables in functions in which they are nested, which makes them quite useful albeit slightly tricky to work with.
  1 件のコメント
Guillaume
Guillaume 2015 年 5 月 20 日
So, to answer the question:
Yes. See the doc

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAdding custom doc についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by