Managing similar apps using inheritance?
6 views (last 30 days)
Show older comments
I need to make approx. 20 apps that share a base set of app designer components and code and then provide extra functionality for a specific purpose. For example: a set of data processing programs with the same base app structure but each customized for a certain experiment. Is there a way to do this using inheritance or similar that would allow changes to the base code to propagate to the children?
The best idea I have is to create a base app and copy/paste that as a starting point for the children. That works for creating the apps but maintaining them could be difficult because every change to the base functionality needs to be repeated for each child.
I haven't found any examples where one app inherited another to extend the functionality. App merging sounded like a possible compromise (by making it easy to merge changes into child apps) but this only works for white editable code blocks and would not handle any changes to UI components or gray code blocks:
0 Comments
Accepted Answer
Lawrence
on 8 Sep 2021
Edited: Lawrence
on 8 Sep 2021
A more supported way of re-using ui elements is by building custom component containers and adding them to your app: https://www.mathworks.com/help/matlab/developing-custom-ui-component-classes.html
That being said, you can subclass from apps built in App Designer, though your subclass needs to be in a ".m" file rather than a ".mlapp" file and can't itself be edited in App Designer. These subclass then unlock many more OOP design patterns. Note that there's no easy way to remove the app.UIFigure.Visible = 'on'; from the end of the superclass constructor, so be careful using this to change visual elements on construction.
subclass.m:
classdef subclass < superclass
methods
function app = subclass
app.SuperclassButton.Text = 'Subclass';
end
end
end
superclass.mlapp:
classdef superclass < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
SuperclassButton matlab.ui.control.Button
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
% Create SuperclassButton
app.SuperclassButton = uibutton(app.UIFigure, 'push');
app.SuperclassButton.Position = [261 278 100 22];
app.SuperclassButton.Text = 'Superclass';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = superclass
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
More Answers (0)
See Also
Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!