Am I to understand that every function in matlab should be its own file?

52 ビュー (過去 30 日間)
Andrew Killian
Andrew Killian 2021 年 9 月 4 日
回答済み: John D'Errico 2021 年 9 月 4 日
It looks like matlab does not use tradional curly brackets to encapsulate a function definition like most programming languages seem to do.
So should I be making a stand-alone matlab file for each function I want to use in a project?

回答 (1 件)

John D'Errico
John D'Errico 2021 年 9 月 4 日
Like SOME languages. Perhaps the ones you know. But certainly not all other languages.
In general, yes, it is not unreasonable to make your functions separate m-files. However, if your main function calls functions that are specific to it, then they can be nested functions or sub-functions, both of which can be contained in the main. These functions are in general not callable from the outside, although that can be accomplished if you return a function handle from the main function.
The use of separate functions has some virtue in that they can then be easily written in advance, then debugge, all as separate functions. As well, those separate functions can now be used to solve multiple problems.

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by