フィルターのクリア

Calling other function from another m file

67 ビュー (過去 30 日間)
Dharmesh Joshi
Dharmesh Joshi 2022 年 12 月 27 日
コメント済み: Voss 2022 年 12 月 28 日
Hi
I am trying to call a function from another .m file.
The primary function which is the same name as the file can be called very easily.
But is it possilbe to call one of the sub function from that same .m file?
Or do i need to place it in its seperate file?
Thanks

採用された回答

the cyclist
the cyclist 2022 年 12 月 27 日
編集済み: the cyclist 2022 年 12 月 27 日
External functions are actually known to MATLAB by their filename, so you do need to put it in a separate file.
  3 件のコメント
Rik
Rik 2022 年 12 月 27 日
Expanding on this answer:
You may notice that a function will still work as expected if you change the name of the main function to no longer match the file name. Problems will start occurring if you put your main function after the first function (this will throw an error or simply not execute).
The mere fact you want to call this subfunction means that it deserves to be a separate function, as it apparently simply an implementation detail for the main function in the file.
the cyclist
the cyclist 2022 年 12 月 27 日
@Dharmesh Joshi, check out this documentation about function precedence, for a full answer to your question.

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2022 年 12 月 27 日
Only the main function in a function file is directly callable from outside the file. Local functions can be called indirectly if you have a function handle to the local function. In order to do this the function handle would need to be created inside the main function and returned from it.
  2 件のコメント
Dharmesh Joshi
Dharmesh Joshi 2022 年 12 月 27 日
Is there an example of this?
Voss
Voss 2022 年 12 月 28 日
@Dharmesh Joshi: See the attached m-file for a simple example of this.
f = main_function
f = function_handle with value:
@local_function
f(4)
ans = 4

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

カテゴリ

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

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by