How can I initialize nested handle class with independent values

Hello, my code is like this:
'ClassA.m'
classdef ClassA < handle
properties
t double = 0
end
end
'ClassB.m'
classdef ClassB < handle
properties
a ClassA = ClassA;
end
end
'TestFun.m'
clear all
test_num = 1e5;
tic
% initialization
class_b(test_num,1) = ClassB;
initialization_a = 0;
if initialization_a % loop assignment of element 'a'
for ii=1:test_num
class_b(ii).a = ClassA;
end
end
toc
% test if each b.a is independent, cause the default value of b.a.t is 0
class_b(1).a.t = 1;
disp(class_b(2).a.t)
Here is the problem description:
Array 'b' (type ClassB,length 1e5) has a member 'a' (type ClassA), they're both handle class.
when initialize 'b', each b.a shares the same value because 'a' is a handle class.
for some reason, i want each b.a have independent value, so i have 2 ways:
1st way is change the classA type as value class,
from "classdef ClassA < handle" to "classdef ClassA"
this will take about 0.15s;
2nd way is enabled loop assignment 'initialization_a = 1' in 'testfun.m'
this will take about 0.70s.
My question is, if i want to use handle class 'a'(2nd way), can i have a faster way to initialize 'a' and have independent b.a value,
the loop assignment seems to be too slow, especially when classA has more member variables.
Thanks a lot.

댓글 수: 1

Thanks, this question and corresponding answers helped me in clarifying my doubts regarding matlab nested objects.

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

 채택된 답변

per isakson
per isakson 2020년 1월 17일
편집: per isakson 2020년 1월 17일
"My question is, if i want to use handle class 'a'(2nd way), can i have a faster way to initialize 'a' and have independent b.a value," AFAIK: The answer is no!
However, see Class object creation performance at UndocumentedMatlab. That article is a bit old and hopefully the performance has been improved since then.
I think the recommended way is as ClassC below, but I didn't find it in the documentation. (I failed to come up with appropriate search terms.)
>> tic, cc = ClassC( 1e5 ); toc
Elapsed time is 0.564107 seconds.
where
classdef ClassC < handle
properties
a ClassA
end
methods
function this = ClassC( n )
if nargin >= 1
this( 1, n ) = ClassC();
for jj = 1 : n
this(jj).a = ClassA();
end
end
end
end
end

댓글 수: 3

Thanks for your advice.
What I don't understand is that, this nested structure of handle class is not unusual for a large project.
Maybe I can only deal with the problem in this way. Thanks ^_^
I'm sure that The Mathworks are aware of this mediocre performance. They have the resources and they made the design decisions based on research. We can just speculate. There has been critisism from the outside regarding the performance based on example with linked list and trees where each node is represented by an object.
My conclusion is that if I chose to use Matlab I need to be aware of this.
The somewhat artifical example below shows the same thing.
>> tic, dd = ClassD( 1e5 ); toc
Elapsed time is 0.156734 seconds.
where
classdef ClassD < handle
properties
a double
end
methods
function this = ClassD( n )
if nargin >= 1
this( 1, n ) = ClassD();
for jj = 1 : n
this(jj).a = jj;
end
end
end
end
end
Yes I totally agree with you, Mathworks is not good enough when dealing with For Loop.
I'm glad to share a case that i met a few days ago, it's about the validator from the property of class.
The code is like this:
classdef ClassA
properties
t (:,1) double
end
end
test_num = 1e5;
class_a = ClassA;
class_a.t = zeros(test_num,1);
tic
for ii=1:test_num
class_a.t(ii) = ii;
end
toc
This will take about 4.0s.
I'm confused by this phenomenon, so i make contact with technical support, and get advice here
If we remove the validator from the property t in ClassA, it will be only 0.04s. :)

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

추가 답변 (1개)

Steven Lord
Steven Lord 2020년 1월 17일

1 개 추천

The section titled "Initializing Properties to Handle Objects" on this documentation page states what happens when you initialize a property containing a handle object in the properties block of a class definition. If you want the properties (containing handle objects) of multiple instances of the same class to be independent rather than all being handles to the same object, assign to it in the constructor.

댓글 수: 1

This page is really helpful for me, it shows me more useful information, Thank you very much!

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

카테고리

도움말 센터File Exchange에서 Properties에 대해 자세히 알아보기

질문:

2020년 1월 15일

댓글:

2020년 6월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by