How can an object array be extented by itself?
古いコメントを表示
I would like an object array to be able to extend itself. I tried the example below but the size of the object array (5x1) does not change to (6x1). Of couse I can append an object to the object array outside, but what about inside the object array?
classdef TestAdd < handle
properties
val=1;
end
methods
function obj=TestAdd(n)
if nargin>0
obj(n,1)=TestAdd;
for ii=1:n
obj(ii,1).val=ii;
end
end
end
function add(obj)
N=length(obj)+1;
obj(end+1,1)=TestAdd;
obj(N,1).val=N;
end
end
end
Command line in/output
>> a=TestAdd(5)
a =
5x1 TestAdd array with properties:
val
>> a.add()
>> a
a =
5x1 TestAdd array with properties:
val
採用された回答
その他の回答 (1 件)
Guillaume
2015 年 10 月 26 日
0 投票
I find matlab's concept that an array of objects is the same type as the objects themselves very very wrong and I would avoid relying on this.
If I were you I would have a second class that is dedicated to handling the array.
I'm not sure why your example fails. My guess would be the array of object isa object concept breaks down when you try to modify the array size outside of the constructor. The doc on array of objects is so sparse that it's difficult to know what is or isn't supposed to work.
カテゴリ
ヘルプ センター および File Exchange で Data Type Identification についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!