How to get property of app object from classdef?

3 次查看(过去 30 天)
Hello,
Typically, I set/get the app object property (text, value) within the code of the app.
I would like to do this stuff from a classdef. Could you please show me how to do it?
Thank you in advance.

回答(1 个)

Deepak
Deepak 2024-10-8
As I understand, you generally modify the properties of the app within the code of the app itself. Now, you want to know how to modify the properties of the app from a user-defined class in App Designer.
To accomplish this task, we must create a class using “classdef” in App Designer that will interact with the app. There should be “get” and “set” methods defined in the class to modify the app properties. Additionally, an instance of the class should be created in the “startupFcnof the app to call the required class functions.
Below is the App Designer code for the same:
classdef AppController
properties
% Define any properties the class might need
end
methods
function obj = AppController()
% Constructor
end
function updateText(app, newText)
% Update the text property of the app
app.UIText.Text = newText;
end
function val = getValue(app)
% Get a value from the app
val = app.propertyName;
end
function setValue(app, newValue)
% Set a value in the app
app.propertyName = newValue;
end
end
end
function startupFcn(app)
% Create an instance of the class
controller = AppController();
% Update the text in the app
controller.updateText(app, 'Hello, World!');
% Get a value from the app
currentValue = controller.getValue(app);
disp(['Current Value: ', num2str(currentValue)]);
% Set a new value in the app
controller.setValue(app, 42);
end
Please find attached the documentation of startupFcnin App Designer for reference:
I hope you found this insightful.

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by