Azzera filtri
Azzera filtri

Managing similar apps using inheritance?

11 visualizzazioni (ultimi 30 giorni)
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:

Risposta accettata

Lawrence il 8 Set 2021
Modificato: Lawrence il 8 Set 2021
A more supported way of re-using ui elements is by building custom component containers and adding them to your app:
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.
classdef subclass < superclass
function app = subclass
app.SuperclassButton.Text = 'Subclass';
classdef superclass < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
SuperclassButton matlab.ui.control.Button
% 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';
% App creation and deletion
methods (Access = public)
% Construct app
function app = superclass
% Create UIFigure and components
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
  1 Commento
Derek Wolfe
Derek Wolfe il 27 Set 2021
Thanks for the info on component containers. I ended up doing something similar where I created plugin classes that can be loaded by the app to add specific functionality. That avoids having to make so many versions of the base app itself

Accedi per commentare.

Più risposte (0)


Scopri di più su Develop uifigure-Based Apps in Help Center e File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by