MATLAB Answers

separate functions or sub-functions

12 views (last 30 days)
George
George on 10 Jun 2013
Dear all,
I have a main function and several sub-functions. Usually, I keep the function and the sub-functions in one .m file. alternatively, I also can save each sub-function as a separate .m file, and then invoke them in the main function.
my question is what the difference between this two way. for example, time consuming, memory needs etc...
thank you. George

Accepted Answer

Matt J
Matt J on 10 Jun 2013
Subfunctions cannot be accessed (without a handle) from outside the mfile. So, if you need (convenient) access to them outside the mfile, it is best not to make it a subfunction.
Subfunctions also do get higher precedence on the search path, but I doubt you would ever see a consequential performance difference because of that.
  2 Comments
Iain
Iain on 12 Jun 2013
If you use NESTED functions, you might notice memory consumption differences.
function func1
function func2
do something
end
func2
end

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!

Translated by