App Designer How to "StartUp" within the App?

20 visualizzazioni (ultimi 30 giorni)
John
John il 21 Apr 2020
Risposto: Tommy il 22 Apr 2020
(1) How to "Restart" the App with something like clicking on a "Quit" Menu item instead of completely Exit the App and re RUN it? That way all properties will be re-initialized as fresh start.
(2) A callback function, can it be called (accessed to) from any function?
Thanks.

Risposta accettata

Tommy
Tommy il 22 Apr 2020
(1) Two possibilities to reset your app to its default state:
I prefer the first. A simple example using a function called resetComponents:
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
EditFieldLabel matlab.ui.control.Label
EditField matlab.ui.control.EditField
Button 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 EditFieldLabel
app.EditFieldLabel = uilabel(app.UIFigure);
app.EditFieldLabel.HorizontalAlignment = 'right';
app.EditFieldLabel.Position = [382 282 56 22];
app.EditFieldLabel.Text = 'Edit Field';
% Create EditField
app.EditField = uieditfield(app.UIFigure, 'text');
app.EditField.Position = [453 282 100 22];
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.Position = [129 282 100 22];
app.Button.Text = 'Reset';
app.Button.ButtonPushedFcn = @app.resetButtonCallback;
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
function resetComponents(app)
% Create EditFieldLabel
app.EditFieldLabel = uilabel(app.UIFigure);
app.EditFieldLabel.HorizontalAlignment = 'right';
app.EditFieldLabel.Position = [382 282 56 22];
app.EditFieldLabel.Text = 'Edit Field';
% Create EditField
app.EditField = uieditfield(app.UIFigure, 'text');
app.EditField.Position = [453 282 100 22];
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.Position = [129 282 100 22];
app.Button.Text = 'Reset';
app.Button.ButtonPushedFcn = @app.resetButtonCallback;
% Or, alternatively, instead of redefining each component, just figure out all
% properties which could be changed by the user and reset them to their
% defaults:
%
% app.EditField.Value = '';
%
% Simpler here, but takes more work for more complicated apps.
end
function resetButtonCallback(app, ~, ~)
resetComponents(app);
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app1
% 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
(2) You can access a callback function if you have access to an instance of the app and you are calling from a location allowed by the function's Access attribute (e.g. Public, Private, etc).
Example 1: You cannot call a callback function from within a static method, because the static method does not have access to an instance of the app.
Example 2: You cannot call a callback function externally if its Access attribute is Private.

Più risposte (0)

Categorie

Scopri di più su Develop Apps Using App Designer in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by