App Designer Tab Duplicated with properties

21 views (last 30 days)
Martin Maurel
Martin Maurel on 25 May 2021
Answered: Allen on 25 May 2021
Hello everyone
I am creating a App via App Designer. I would like to be able to duplicate a tab and all its property ( CallBack etc).
Currently I am able to duplicate it but the script is very heavy...However, I don't know how to copy callbackS too.
If you have an idea?
classdef CodeForum < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
NumberOfStage matlab.ui.control.NumericEditField
NumberofStageLabel matlab.ui.control.Label
TabGroup matlab.ui.container.TabGroup
Etage1Tab matlab.ui.container.Tab
ResiduB3 matlab.ui.control.TextArea
Residualbeta_3ps3Label matlab.ui.control.Label
ResiduB2 matlab.ui.control.TextArea
Residualbeta_2ps2TextAreaLabel matlab.ui.control.Label
LengthAfter matlab.ui.control.NumericEditField
LengthmEditField_2Label matlab.ui.control.Label
LengthBefore matlab.ui.control.NumericEditField
LengthmEditFieldLabel matlab.ui.control.Label
PassiveFiberAfterButton matlab.ui.control.StateButton
PassiveFiberBeforeButton matlab.ui.control.StateButton
OpticDropDown matlab.ui.control.DropDown
OpticDropDownLabel matlab.ui.control.Label
end
properties (Access = public)
c= 299792458 ;% Description
h = 6.626e-34; % Constante de Planck [Js]
kb=1.38064852e-23; % Constante de Boltzman
end
properties (Access = private)
tabs = cell(1,100);
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: NumberOfStage
function NumberOfStageValueChanged(app, event)
value = app.NumberOfStage.Value;
app.TabGroup.Parent=[];
% app.TabGroup = uitabgroup(app.UIFigure);
% app.TabGroup.Position = [417 374 314 262];
% app.Etage1Tab = uitab(app.TabGroup);
% app.Etage1Tab.Title = 'Etage1';
% app.TabGroup = uitabgroup(app.UIFigure);
% app.TabGroup.Position = [417 505 314 262];
app.Etage1Tab = uitab(app.TabGroup);
% Create TabGroup
app.TabGroup = uitabgroup(app.UIFigure);
app.TabGroup.Position = [132 180 314 262];
% Create Etage1Tab
app.Etage1Tab = uitab(app.TabGroup);
app.Etage1Tab.Title = 'Etage1';
% Create OpticDropDownLabel
app.OpticDropDownLabel = uilabel(app.Etage1Tab);
app.OpticDropDownLabel.HorizontalAlignment = 'right';
app.OpticDropDownLabel.Position = [6 165 34 22];
app.OpticDropDownLabel.Text = 'Optic';
% Create OpticDropDown
app.OpticDropDown = uidropdown(app.Etage1Tab);
app.OpticDropDown.Items = {'FBG', 'Ampli Fiber', 'Passive Fiber'};
app.OpticDropDown.Position = [54 165 116 22];
app.OpticDropDown.Value = 'FBG';
% Create PassiveFiberBeforeButton
app.PassiveFiberBeforeButton = uibutton(app.Etage1Tab, 'state');
app.PassiveFiberBeforeButton.Text = 'Passive Fiber Before';
app.PassiveFiberBeforeButton.Position = [1 207 127 22];
% Create PassiveFiberAfterButton
app.PassiveFiberAfterButton = uibutton(app.Etage1Tab, 'state');
app.PassiveFiberAfterButton.Text = 'Passive Fiber After';
app.PassiveFiberAfterButton.Position = [1 12 116 22];
% Create LengthmEditFieldLabel
app.LengthmEditFieldLabel = uilabel(app.Etage1Tab);
app.LengthmEditFieldLabel.HorizontalAlignment = 'right';
app.LengthmEditFieldLabel.Position = [149 207 62 22];
app.LengthmEditFieldLabel.Text = 'Length [m]';
% Create LengthBefore
app.LengthBefore = uieditfield(app.Etage1Tab, 'numeric');
app.LengthBefore.Position = [214 207 35 22];
% Create LengthmEditField_2Label
app.LengthmEditField_2Label = uilabel(app.Etage1Tab);
app.LengthmEditField_2Label.HorizontalAlignment = 'right';
app.LengthmEditField_2Label.Position = [149 12 62 22];
app.LengthmEditField_2Label.Text = 'Length [m]';
% Create LengthAfter
app.LengthAfter = uieditfield(app.Etage1Tab, 'numeric');
app.LengthAfter.Position = [214 12 35 22];
% Create Residualbeta_2ps2TextAreaLabel
app.Residualbeta_2ps2TextAreaLabel = uilabel(app.Etage1Tab);
app.Residualbeta_2ps2TextAreaLabel.Interpreter = 'latex';
app.Residualbeta_2ps2TextAreaLabel.HorizontalAlignment = 'right';
app.Residualbeta_2ps2TextAreaLabel.Position = [54 138 96 22];
app.Residualbeta_2ps2TextAreaLabel.Text = 'Residual $ \beta_2 [ps^2]$';
% Create ResiduB2
app.ResiduB2 = uitextarea(app.Etage1Tab);
app.ResiduB2.Position = [159 137 41 24];
app.ResiduB2.Value = {'0'};
% Create Residualbeta_3ps3Label
app.Residualbeta_3ps3Label = uilabel(app.Etage1Tab);
app.Residualbeta_3ps3Label.Interpreter = 'latex';
app.Residualbeta_3ps3Label.HorizontalAlignment = 'right';
app.Residualbeta_3ps3Label.Position = [39 113 111 22];
app.Residualbeta_3ps3Label.Text = 'Residual $ \beta_3 [ps^3]$';
% Create ResiduB3
app.ResiduB3 = uitextarea(app.Etage1Tab);
app.ResiduB3.Position = [159 113 41 20];
app.ResiduB3.Value = {'0'};
app.Etage1Tab.Title = 'Etage1';
app.TabGroup
for i = 2: value %create the number of tabs entered by the user
app.tabs{i} = uitab(app.TabGroup,'Title',['Etage' num2str(i)]);
% Create OpticDropDownLabel
app.OpticDropDownLabel = uilabel(app.tabs{i});
app.OpticDropDownLabel.HorizontalAlignment = 'right';
app.OpticDropDownLabel.Position = [6 165 34 22];
app.OpticDropDownLabel.Text = 'Optic';
% Create OpticDropDown
app.OpticDropDown = uidropdown(app.tabs{i});
app.OpticDropDown.Items = {'FBG', 'Ampli Fiber', 'Passive Fiber'};
app.OpticDropDown.ValueChangedFcn = createCallbackFcn(app, @OpticDropDownValueChanged, true);
app.OpticDropDown.Position = [54 165 116 22];
app.OpticDropDown.Value = 'FBG';
% Create PassiveFiberBeforeButton
app.PassiveFiberBeforeButton = uibutton(app.tabs{i}, 'state');
app.PassiveFiberBeforeButton.Text = 'Passive Fiber Before';
app.PassiveFiberBeforeButton.Position = [1 207 127 22];
% Create PassiveFiberAfterButton
app.PassiveFiberAfterButton = uibutton(app.tabs{i}, 'state');
app.PassiveFiberAfterButton.Text = 'Passive Fiber After';
app.PassiveFiberAfterButton.Position = [1 12 116 22];
% Create LengthmEditFieldLabel
app.LengthmEditFieldLabel = uilabel(app.tabs{i});
app.LengthmEditFieldLabel.HorizontalAlignment = 'right';
app.LengthmEditFieldLabel.Position = [149 207 62 22];
app.LengthmEditFieldLabel.Text = 'Length [m]';
% Create LengthBefore
app.LengthBefore = uieditfield(app.tabs{i}, 'numeric');
app.LengthBefore.Position = [214 207 35 22];
% Create LengthmEditField_2Label
app.LengthmEditField_2Label = uilabel(app.tabs{i});
app.LengthmEditField_2Label.HorizontalAlignment = 'right';
app.LengthmEditField_2Label.Position = [149 12 62 22];
app.LengthmEditField_2Label.Text = 'Length [m]';
% Create LengthAfter
app.LengthAfter = uieditfield(app.tabs{i}, 'numeric');
app.LengthAfter.Position = [214 12 35 22];
% Create Residualbeta_2ps2TextAreaLabel
app.Residualbeta_2ps2TextAreaLabel = uilabel(app.tabs{i});
app.Residualbeta_2ps2TextAreaLabel.Interpreter = 'latex';
app.Residualbeta_2ps2TextAreaLabel.HorizontalAlignment = 'right';
app.Residualbeta_2ps2TextAreaLabel.Position = [54 138 96 22];
app.Residualbeta_2ps2TextAreaLabel.Text = 'Residual $ \beta_2 [ps^2]$';
% Create ResiduB2
app.ResiduB2 = uitextarea(app.tabs{i});
app.ResiduB2.Position = [159 137 41 24];
app.ResiduB2.Value = {'0'};
% Create Residualbeta_3ps3Label
app.Residualbeta_3ps3Label = uilabel(app.tabs{i});
app.Residualbeta_3ps3Label.Interpreter = 'latex';
app.Residualbeta_3ps3Label.HorizontalAlignment = 'right';
app.Residualbeta_3ps3Label.Position = [39 113 111 22];
app.Residualbeta_3ps3Label.Text = 'Residual $ \beta_3 [ps^3]$';
% Create ResiduB3
app.ResiduB3 = uitextarea(app.tabs{i});
app.ResiduB3.Position = [159 113 41 20];
app.ResiduB3.Value = {'0'};
% uitextarea(app.tabs{i})
i
end
end
% Value changed function: OpticDropDown
function OpticDropDownValueChanged(app, event)
value = app.OpticDropDown.Value;
if isequal(value , 'FBG')
app.ResiduB2.Visible = 'on'
app.Residualbeta_2ps2TextAreaLabel.Visible = 'on'
app.ResiduB3.Visible = 'on'
app.Residualbeta_3ps3Label.Visible = 'on'
elseif isequal(value , 'Ampli Fiber')
app.ResiduB2.Visible = 'off'
app.Residualbeta_2ps2TextAreaLabel.Visible = 'off'
app.ResiduB3.Visible = 'off'
app.Residualbeta_3ps3Label.Visible = 'off'
elseif isequal(value , 'Passive Fiber')
app.ResiduB2.Visible = 'off'
app.Residualbeta_2ps2TextAreaLabel.Visible = 'off'
app.ResiduB3.Visible = 'off'
app.Residualbeta_3ps3Label.Visible = 'off'
end
end
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 = 'MATLAB App';
% Create TabGroup
app.TabGroup = uitabgroup(app.UIFigure);
app.TabGroup.Position = [132 180 314 262];
% Create Etage1Tab
app.Etage1Tab = uitab(app.TabGroup);
app.Etage1Tab.Title = 'Etage1';
% Create OpticDropDownLabel
app.OpticDropDownLabel = uilabel(app.Etage1Tab);
app.OpticDropDownLabel.HorizontalAlignment = 'right';
app.OpticDropDownLabel.Position = [6 165 34 22];
app.OpticDropDownLabel.Text = 'Optic';
% Create OpticDropDown
app.OpticDropDown = uidropdown(app.Etage1Tab);
app.OpticDropDown.Items = {'FBG', 'Ampli Fiber', 'Passive Fiber'};
app.OpticDropDown.ValueChangedFcn = createCallbackFcn(app, @OpticDropDownValueChanged, true);
app.OpticDropDown.Position = [54 165 116 22];
app.OpticDropDown.Value = 'FBG';
% Create PassiveFiberBeforeButton
app.PassiveFiberBeforeButton = uibutton(app.Etage1Tab, 'state');
app.PassiveFiberBeforeButton.Text = 'Passive Fiber Before';
app.PassiveFiberBeforeButton.Position = [1 207 127 22];
% Create PassiveFiberAfterButton
app.PassiveFiberAfterButton = uibutton(app.Etage1Tab, 'state');
app.PassiveFiberAfterButton.Text = 'Passive Fiber After';
app.PassiveFiberAfterButton.Position = [1 12 116 22];
% Create LengthmEditFieldLabel
app.LengthmEditFieldLabel = uilabel(app.Etage1Tab);
app.LengthmEditFieldLabel.HorizontalAlignment = 'right';
app.LengthmEditFieldLabel.Position = [149 207 62 22];
app.LengthmEditFieldLabel.Text = 'Length [m]';
% Create LengthBefore
app.LengthBefore = uieditfield(app.Etage1Tab, 'numeric');
app.LengthBefore.Position = [214 207 35 22];
% Create LengthmEditField_2Label
app.LengthmEditField_2Label = uilabel(app.Etage1Tab);
app.LengthmEditField_2Label.HorizontalAlignment = 'right';
app.LengthmEditField_2Label.Position = [149 12 62 22];
app.LengthmEditField_2Label.Text = 'Length [m]';
% Create LengthAfter
app.LengthAfter = uieditfield(app.Etage1Tab, 'numeric');
app.LengthAfter.Position = [214 12 35 22];
% Create Residualbeta_2ps2TextAreaLabel
app.Residualbeta_2ps2TextAreaLabel = uilabel(app.Etage1Tab);
app.Residualbeta_2ps2TextAreaLabel.Interpreter = 'latex';
app.Residualbeta_2ps2TextAreaLabel.HorizontalAlignment = 'right';
app.Residualbeta_2ps2TextAreaLabel.Position = [54 138 96 22];
app.Residualbeta_2ps2TextAreaLabel.Text = 'Residual $ \beta_2 [ps^2]$';
% Create ResiduB2
app.ResiduB2 = uitextarea(app.Etage1Tab);
app.ResiduB2.Position = [159 137 41 24];
app.ResiduB2.Value = {'0'};
% Create Residualbeta_3ps3Label
app.Residualbeta_3ps3Label = uilabel(app.Etage1Tab);
app.Residualbeta_3ps3Label.Interpreter = 'latex';
app.Residualbeta_3ps3Label.HorizontalAlignment = 'right';
app.Residualbeta_3ps3Label.Position = [39 113 111 22];
app.Residualbeta_3ps3Label.Text = 'Residual $ \beta_3 [ps^3]$';
% Create ResiduB3
app.ResiduB3 = uitextarea(app.Etage1Tab);
app.ResiduB3.Position = [159 113 41 20];
app.ResiduB3.Value = {'0'};
% Create NumberofStageLabel
app.NumberofStageLabel = uilabel(app.UIFigure);
app.NumberofStageLabel.FontColor = [0.149 0.149 0.149];
app.NumberofStageLabel.Position = [171 115 96 22];
app.NumberofStageLabel.Text = 'Number of Stage';
% Create NumberOfStage
app.NumberOfStage = uieditfield(app.UIFigure, 'numeric');
app.NumberOfStage.ValueChangedFcn = createCallbackFcn(app, @NumberOfStageValueChanged, true);
app.NumberOfStage.HorizontalAlignment = 'center';
app.NumberOfStage.FontColor = [0.149 0.149 0.149];
app.NumberOfStage.Position = [318 115 40 22];
app.NumberOfStage.Value = 1;
% 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 = CodeForum
% 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
Regards

Answers (1)

Allen
Allen on 25 May 2021
MATLAB requires that you manually create a callback for each object in App Designer. I say manually, but this is simply done by using the Component Brower or other similar method. App Design generates the callback function with default inputs.
Other methods to add callbacks.
However, if your end goal is to duplicate the exact callback behavior for more than one object, you can assign a callback function to multiple objects via the Component Browser as well. Select the object handle in the Component Browser for the desired object that you would like change its callback. Toggle to the "Callbacks" tab from the "Inspector" tab. Select the desired callback to assign to current object. Repeat this for as many objects that you want. In the example below, I have several objects that run the CalculateCGValues callback function whenever their value is changed. I also renamed this callback to something that is more descriptive instead of keeping the original name (e.g. num_DimQValueChanged).
Above the callback function handle code in the Code View window, App Designer lists all of the objects that are the callback and for what behavior. This example shows that objects dd_AxleSelect, num_AxleElevHt, num_AxleWtFront, num_AxleWtRear, num_AxleWtRest, num_DimE, and num_DimQ all use the CalculateCGValues callback function when they experience a value change.

Community Treasure Hunt

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

Start Hunting!

Translated by