Create an array of distinct objects of handle class
Afficher commentaires plus anciens
I want to store objects of a handle class in an array.
When using
for i=1:2
arr(i) = uilabel;
end
arrFor(1).Text = "textFor 1";
arr(2).Text = "textFor 2";
the objects in the array are distinct.
When using
arrDir(1:2) = uilabel;
arrDir(1).Text = "textDir 1";
arrDir(2).Text = "textDir 2";
both text will change to "textDir 2" which makes sense because uilabel is a handle class.
What is the syntax to avoid the for loop and diretly crating an array of distinct objects of a handle class?
2 commentaires
Steven Lord
le 19 Avr 2024
Why is the for loop not a desirable solution for you?
Michael
le 19 Avr 2024
Réponse acceptée
Plus de réponses (1)
Fangjun Jiang
le 19 Avr 2024
1 vote
As long as uilable() can only create a single object, there is no way to avoid the loop.
In arrDir(1:2) = uilabel, the same single handle was assigned to the two elements of arrDir.
What you should look for, is whatever the object class that you are working on, whether its creation method has the capability or syntax to create multiple objects in one shot.
3 commentaires
Matt J
le 19 Avr 2024
As long as uilable() can only create a single object, there is no way to avoid the loop.
And there is no way for a handle class constructor to create multiple instances. So, all solutions will be loop-equivalent.
Bruno Luong
le 19 Avr 2024
Modifié(e) : Bruno Luong
le 19 Avr 2024
Well a lot pf people use object (graphical) handles for decades, I don't see that often the need of deep copying handls or clonig them to an array.
If they do often the constructor is invoked as many as it needs, whereas it is manually unroll out, inside a for-loop or wrapped into an arrayfun/cellfun.
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!