How to initialize a vector of class handles?

前端 未结 3 1999
渐次进展
渐次进展 2021-01-14 05:04

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

相关标签:
3条回答
  • 2021-01-14 05:50
    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).

    0 讨论(0)
  • 2021-01-14 05:57

    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
    
    0 讨论(0)
  • 2021-01-14 05:58

    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{:}];
    
    0 讨论(0)
提交回复
热议问题