I have a handle-based class that I need to create a vector of. An easy method of doing this is to dynamically construct the vector in a for loop, but this causes mlint
foo(10) = HandleClass();
This will default fill foo(1) through foo(9).
Note that this only works if HandleClass's constructor works with no input arguments (that is, it can be default-constructed).
Seems you can do this by a call to the empty method that is present in all non-abstract classes.
foo = HandleClass.empty(10,0);
for i = 1:10
foo(i) = HandleClass();
end
Having a default constructor the accepted answer is fine. Having no default constructor (HandleClass()
) returns not enough input arguments) the best possibility I see is creating a cell first:
foo=cell(1,10);
for ix=1:10
foo{ix}=HandleClass(ix)
end;
foo=[foo{:}];