Effacer les filtres
Effacer les filtres

Referring to object names in App Designer as elements of an array

34 vues (au cours des 30 derniers jours)
Saeid
Saeid le 30 Juil 2024 à 10:47
Commenté : Voss le 30 Juil 2024 à 15:38
In App Designer, I want to introduce the names of a series of objects (e.g. uifigures) in an array, and then refer to them by their index. For example:
PlotNames={'app.XY1' 'app.XY2' 'app.XY3'}
for iPN=1:length(PlotNames)
plot(PlotNames{iPN},X(iPN),Y(iPN))
end
but when I do this I get the error: 'Invalid first data argument.'
Is it possible to refer to object names in App Designer as elements of an array?

Réponses (2)

Steven Lord
Steven Lord le 30 Juil 2024 à 14:19
You can access properties of an object using a variable name using the syntax shown in the Reference Properties Using Variables section on this documentation page.
  1 commentaire
Voss
Voss le 30 Juil 2024 à 15:38
@Saeid: Your example code adapted to this approach would look like this:
PlotNames = {'XY1' 'XY2' 'XY3'};
for iPN = 1:numel(PlotNames)
plot(app.(PlotNames{iPN}),X(iPN),Y(iPN))
end

Connectez-vous pour commenter.


Aditya
Aditya le 30 Juil 2024 à 10:53
Hi Saeid,
In MATLAB, you cannot directly store object names as strings in an array and then refer to them. Instead, you can store the object handles directly in an array. This allows you to refer to the objects by their index without encountering errors.
Updated code:
% Assuming XY1, XY2, and XY3 are UIAxes in your app
PlotHandles = {app.XY1, app.XY2, app.XY3};
for iPN=1:length(PlotNames)
plot(PlotNames{iPN},X(iPN),Y(iPN))
end
I hope this solves your issue!

Catégories

En savoir plus sur Develop uifigure-Based Apps dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by