MATLAB Answers

GUI: Call external functions or define them within a GUI?

6 views (last 30 days)
Matthias on 14 Dec 2016
Commented: Matthias on 15 Dec 2016
Hi, I'd like to know if calling an external function in a GUI is considerably slower than defining the function within the GUI and then calling it. I don't like to have my GUI stuffed with functions when I could simply put them in a .m file in the GUIs folder. Are there any disadvantages to this approach though?

Accepted Answer

David Barry
David Barry on 14 Dec 2016
The short answer is no there is no performance issue here and splitting up functionality into separate functions should be encouraged, especially if they are reusable components. Having a single GUI function quickly becomes out of hand so you should always spend time upfront thinking about your software architecture before you start writing code.

More Answers (1)

Image Analyst
Image Analyst on 15 Dec 2016
I guess it's a matter of preference. I prefer a lot of functions in my GUI m-file, unless they're general purpose utilities to be used by a lot of programs. But if it's a function that is used only by that GUI, I just put them all in the same file. That way I can easily go to the function by using the GoTo drop down list on the tool ribbon, and all the functions are listed all the time, unlike if your functions are in files that you have not opened yet. And if I type control-D to go to a function, it also leaves me in the same editor window, which cuts down on the number of editor windows (tabs) that I have open (less clutter). I go to so many functions when I'm editing a bug program (dozens) that if every time I went to one it opened a new editor window and threw me over there it would get confusing. Bookmarks I had set in the main program don't work anymore when I'm in another editor file, etc. And if you have more than about 10 editor windows open you can no longer see them all and you have to go over to the little down arrow to select the window you need. It's just a cluttered pain. Searching is a little easier if everything is in one file since you can type control-F instead of control-shift-f which takes extra dexterity or the use of two hands instead of one. So I find it easier to put them all in the same m-file. For me, it's easier to have one file, one editor window, one set of bookmarks, easier searching, less cluttered interface, etc.
  1 Comment
Matthias on 15 Dec 2016
Good point. In my case I only have to deal with about 5 functions, but each 200+ lines. So for me having them as external functions is more convenient. It's also good when people want to have a look at what you have done in a certain function.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!