MATLAB: How to solve preallocating speed of a variable

class objectmethodsobject variableobject-orientedoopproperties

I have a variable which is object structure, I mean I define all properties in an m.file classfile and properties structure. Then I use them with a variable for example;
classdef obj<handle>
properties
id = zeros;
end
end
Then, I use for example "car" list in the for loop to fill the all properties, the number of properties is 30.
car = [];
for segment=1:5
for brand = 1:6
car(segment,brand).id = list(brand,'id');
..... till 30 properties like above
end
end
It works but the MATLAB gives suggestion to speed the script. The error*;
*The size of the indicated variable or array appears to be changing with each loop iteration. Commonly, this message appears because an array is growing by assignment or concatenation. Growing an array by assignment or concatenation can be expensive. For large arrays, MATLAB must allocate a new block of memory and copy the older array contents to the new array as it makes each assignment.
*Programs that change the size of a variable in this way can spend most of their run time in this inefficient activity. There is also significant overhead in shrinking an array on each iteration or in changing the size of a variable on each iteration. In such cases, MATLAB must reallocate and copy the contents repeatedly.
*For scripts, Code Analyzer cannot determine whether the array was preallocated before the script was called. Depending on your use of scripts, you might want to enable or disable this message independently of the related message (with message ID AGROW) that applies to functions and class methods.
How to make this done, variable has two indices like car(segment,brand).

Best Answer

One simple solution is given here:
"To preallocate the object array, assign the last element of the array first."
You will have to make sure that your class returns a default value.