Include function in gui m-file or separate them?
2 views (last 30 days)
Show older comments
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?
- Indifferent?
Thanks
0 Comments
Accepted Answer
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.
2 Comments
Image Analyst
on 8 Jan 2014
Edited: Image Analyst
on 8 Jan 2014
Well, you're right and you're not right. If you do a save to another filename in GUIDE, it will create a duplicate of your file, which includes all GUIDE-generated functions and, very importantly, including any custom functions , with all the things renamed inside that need to be renamed. No porting of custom written functions is needed. I do this all the time when I make a new project that is similar to an old project and so, can be based on that project as a starting point, just with a different name.
Matt J
on 8 Jan 2014
Glad to know that, but then how am I both "right and not right"? Sounds like I was all wrong!
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.
0 Comments
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.
1 Comment
Image Analyst
on 8 Jan 2014
I'm of the opposite opinion. Unless a function is a utility used by other m-files, I keep my functions in the same m-file. I use GUIDE so there are already dozens of functions in there anyway . When I'm editing I typically have to edit lots of functions that are used only in that m-file. Rather than "a nightmare", I find it easy to just do control-F to find some function that I'm looking for. Otherwise I'd have to search dozens of tabbed editor windows for the file I'm looking for, which can sometimes involved scrolling the display to find it, or if it's not found among the already opened files, to then activate the current folder panel, and then double click on it in the list of files to open it into an editor window. I think that would be the nightmare. I prefer to have it there already. Though if the function I'm looking for is called on the current screen I can just type control-d to get there instead of control-f and typing the name. Like I said, my opinion, and I'm not trying to change yours or change the way you like to do things.
The main m-file I'm working on now has 92 functions in a single 6000 line long file. I can't imagine having 92 separate m-files. To me that would seem more complicated, flitting between 92 different files. The whole project has 3 main m-files, and about 5 utility m-files that are used by more than one m-file, so my folder looks very lean and easy to find things. Compare that to a folder with 300 different files.
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!