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

18 Ansichten (letzte 30 Tage)
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
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
I enter the code in TextArea and click on the button.
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';
Nothing happens.
% Button pushed function: Button2
function Button2Pushed2(app, event)
app.Voltage.Title = '123';
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.

Antworten (1)

Chris Portal
Chris Portal am 31 Mai 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:
  3 Kommentare
Chris Portal
Chris Portal am 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)

Melden Sie sich an, um zu kommentieren.


Mehr zu Create Custom UI Components finden Sie in Help Center und File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by