I've found, as my projects get bigger, it is easier to use a MATLAB class to control and manage a GUI instead of manipulating the standard Matlab gui mfiles.
I believe this style of GUI programming makes passing data around from different areas of gui much easier.
The functionality of this example is analogous to the built in Matlab example in GUIDE. This example is purely to show another way to solve the same problem. This example can be extended to many other applications.
Running command: gui_class_example()
% This exmaple shows how to use a MATLAB CLASSDEF to create, maintain, and
% destroy a gui.
% I chose to use one of the standard MATLAB guide default gui examples,
% this allows you to see the differences between the two methods.
% I prefer using classes to control gui's because as the project gets
% larger, I find it much easier to maintain, understand, and debug using a
% class bases sytem than using the traditional gui based system.
% Also, this method is great for passing data between elements of the gui.
% Since the class managed the gui, all gui elems are within memory scope of
% the class.
% Almost everything is the same between managing a gui through the gui
% mfile and a class. There are 2 main differences that I've noticed.
% 1. With the class based system, you do not need to store and set the
% guidata to obtain and pass data along
% 2. Cleanup is harder using the Class based system. There are 2 objects in
% memory, the gui itself and the class. These must be linked in some way
% that if one is closed or destroyed, the other is taken care of. I show
% one solution to this siutation here by adding a closerequestfcn to the
% figure. This function then calls the class's delete function to clean up
% the memory (preventing memory leaks).
Alex (2022). Example using a MATLAB Class to control a GUI (https://www.mathworks.com/matlabcentral/fileexchange/33816-example-using-a-matlab-class-to-control-a-gui), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform CompatibilityWindows macOS Linux
Inspired: Model-View-Presenter example GUI
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!