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

11 次查看(过去 30 天)
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.

回答(1 个)

Chris Portal
Chris Portal 2018-5-31
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 个评论
Chris Portal
Chris Portal 2018-11-15
"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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 App Building 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by