Create an array of distinct objects of handle class

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

Why is the for loop not a desirable solution for you?
Because it is bulky, clutters the code and not very efficient.

댓글을 달려면 로그인하십시오.

 채택된 답변

Bruno Luong
Bruno Luong 2024년 4월 19일
편집: Bruno Luong 2024년 4월 19일
Why not simply calling the constructor as many time as you need
arrDir = arrayfun(@(~)uilabel, 1:2);
arrDir(1).Text = "textDir 1";
arrDir(2).Text = "textDir 2";
arrDir(1).Text

댓글 수: 4

Michael
Michael 2024년 4월 19일
편집: Michael 2024년 4월 19일
Thank you.
I am aware of that solution but want to avoid it as well. This is basically the same as the for loop but in a more compact form.
Is there another possibility to use a more 'native' assignment?
Well here is, lust a longer and longer code
copyhandle = @(h,varargin) arrayfun(@(~)copy(h), zeros(varargin{:}))
h = uilabel();
arrDir = copyhandle(h, 1, 2); % copy h to an array of size 1 x 2
arrDir(1).Text = "textDir 1";
arrDir(2).Text = "textDir 2";
arrDir(1).Text
You could probably declare a subclass and over load the subsref subsasg methods and erase the handle copy behavior.
First of all I never did such thing. Second: it is just adding layer on layer and tweak the initial design pattern. It become dirty diffuclt to maibtain understand the code.
Under the hood this is basically the same thing as the for loop with the drawback of more difficult debugging but more compact code. Regarding the other answers this seem to be the best option. Thank you.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2024년 4월 19일

1 개 추천

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

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.
Michael
Michael 2024년 4월 19일
편집: Michael 2024년 4월 19일
This answers the question. It is not what I hoped but in that case arrayfun seems to be the cleanest way to handle this case.
Do you know why there is no such functionality? Seems like there might be plenty of usecases where this could be really helpful, especially when dealing with user interfaces.
Is there some underlying Matlab voodoo why this does not male sense?
Bruno Luong
Bruno Luong 2024년 4월 19일
편집: Bruno Luong 2024년 4월 19일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2023a

질문:

2024년 4월 19일

편집:

2024년 4월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by