I have a self defined class ClassA
, and I want to create an empty object array of size N
to store N
ClassA
objects.
Right now I am using an empty cell array cellarrayA = cell(N,1)
, and putting each object into the cell, like cellarrayA(n) = ClassA(input(n))
. And once they're all done I turn the cell array into object array using objarrayA = [cellarrayA{:}]
.
It works (Matlab does not complain) but I don't think it actually preallocate the right amount of memory, because how can a cell array know the size of my object before it's created? The size of my object could be fairly large, on the order of 1MB (but it could vary). I would guess I might be suffering the same performance penalty just as if I'm not preallocating anything at all, although I won't be able to verify it. So how can I preallocate the object array, instead of using the cell array?