Introduction of a new class properties inside its functions in App Designer.

11 views (last 30 days)
I want the user to be able to add new UI components to the program.
For example, I want the user to enter code in TextArea that creates a new Panel (Voltage) and that can be accessed by using the app.Voltage.
The program has a TextArea and Button.
classdef app < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
TextArea matlab.ui.control.TextArea
Button matlab.ui.control.Button
end
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
n = str2num(app.TextArea.Value{1}); % In the first line of TextArea writes the number of lines.
eval([app.TextArea.Value{2:n};]); % All other lines are executed using the Eval function
end
end
...
I enter the code in TextArea and click on the button.
2
Voltage = uipanel(app.UIFigure)
A new panel is created.
But I can not address to it through other functions by using "Voltage" or "app.Voltage".
% Button pushed function: Button2
function Button2Pushed(app, event)
Voltage.Title = '123';
end
Nothing happens.
% Button pushed function: Button2
function Button2Pushed2(app, event)
app.Voltage.Title = '123';
end
Error: No appropriate method, property, or field 'Voltage' for class 'app'.
The last one is solved simply, it is necessary to add in Properties: "Voltage matlab.ui.container.Panel"
The problem in that what I can not do this, becouse I do not know the name and the UI component which will be added in advance.

Answers (1)

Chris Portal
Chris Portal on 31 May 2018
The way to do this would be to add a property to your app called something like “NewComponents”.
Every time you need to introduce a new component, create it and store the handle as a field of your NewComponents property. Something like:
NewComponents.Voltage = uipanel(app.UIFigure);
Then, whenever you need to access one of these components, access it using the property you created:
NewComponents.Voltage
  3 Comments
Chris Portal
Chris Portal on 15 Nov 2018
"qwer ty", the code you showed should work, but only if you defined NewComponents as a property of your app. If it's not a property of the app, then the panel handle isn't being saved anywhere once your Button 1 callback exits, which would explain why the code in Button 2's callback isn't doing anything.
The way to add a property to your app is to have this in your properties block:
properties (Access = public)
NewComponents
end

Sign in to comment.

Categories

Find more on App Building in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!