Include function in gui m-file or separate them?
20 views (last 30 days)
I have a GUI, and its m-file calls several functions What is better (faster)?
- Save functions called by GUI in separate m-files. This way the GUI's m-file is shorter, but there will be lots of m-files.
- Include functions inside GUI's m-file. This way that file is long, but there are no other m-files to call. Maybe this way the execution is faster?
More Answers (3)
Matt J on 8 Jan 2014
Edited: Matt J on 8 Jan 2014
I don't think there will be a significant speed difference.
I assume you're doing this in GUIDE and that GUIDE has generated a gui mfile for you that already contains lots of functions (call backs etc...).
If you're thinking about adding your own additional functions to this mfile that GUIDE cannot see, there could be problems later if you want to rename the GUI. If you re-apply GUIDE to the renamed template, I'm pretty sure it will generate you a fresh mfile without your additional functions. You will have to port them all over again manually.
Maybe that's not a big problem, if you keep them in a designated section of the mfile that's easy to copy/paste.
Jan on 8 Jan 2014
For a cleaner structure I would allways outsource functionality in seperate .m files. For me (in Matlab as in any other programming language) the GUI class/functions should never contain code, that does anything, but offer user access to a seperate function layer.
David Sanchez on 8 Jan 2014
For maintenance and readability, it is a much better practice to have your functions in separated m-files. You never know when you have to modify one function to add/delete functionalities. In speed terms, you are not going to perceive the difference. Trying to locate your functions in a single "endless" m-file might become a nightmare if you have to do it often, while having your function easily available in its own file is a matter of naming in a good way.