oop - Matlab: Instantiate the handle class inside a function within another class -
let's say, instance, have 2 classes: & b. have set b handle class , property instantiate class (i.e. b).
therefore, have done in class a:
% constructor function = a() a.objb = b(); % works fine ... = 1:10 a.var(i) = b(); % causes error occur end end
the error listed below:
"error using double conversion double b not possible.
the code snippet inside loop seems work if change a.var(i) = b();
var(i) = b();
.
do have idea why is?
your .var
field initialized double when make assignment (maybe []
). using a.var(i) = xxx
cannot change type of a.var
.
try resetting value first time used. eg
for = 1:10 if == 1 a.var = b(); % overwrite existing value else a.var(i) = b(); % append value end end
this cause a.var
field reallocated every loop. pre-allocated array make go faster. easiest way pre-allocate loop backwards, this:
for = 10:-1:1 if == 10 a.var = b(); % overwrite existing value, allocated room 10 elements else a.var(i) = b(); % assign each element in turn, 9 through 1 end end
Comments
Post a Comment