How to get property of app object from classdef?

1 view (last 30 days)
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.

Answers (1)

Deepak
Deepak on 8 Oct 2024
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.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!