Like any MATLAB array, it is bad to iteratively concatenate to a struct array because it forces lots of unnecessary memory reallocation. It would be better if you pre-allocate the array by doing
N=length(input);
struct_array(N)=struct('field1",[],'field2',[]);
for i=1:N
struct_array(i)=struct_fn(input(i));
end
Alternatively, instead of pre-allocating before the loop, you could also loop backwards so that all memory allocation is done in the first iteration of the loop.
for i=N:-1:1
struct_array(i)=struct_fn(input(i));
end
Best Answer