How do I pre-allocate memory for a structure?
169 views (last 30 days)
Can anyone help me to fix pre-allocation memory for a structure output?
I have a structure function which have 18 elements, it has two imputs and give results 16 outputs and two inputs.
If I want to pre-allocate the output memory how can I do it.
Here is the code:
phase_in = ?????; % what should be in question marks ( I did zeros(length(v_in),refprop))
phase_in(i) = refprop(v_in(i),T_in,r410a);
r410 is my structure function, and v_in and T_in are inputs, I will calculate 100 times to calculate phase_in.
If I skip pre-allocation, the output will give me 1*1000 structure. In this case, I tried to pre-allocate phase_in=zeros(100,18),
but it gave me error'' Conversion to double from struct is not possible.''
Thanks a lot in advance.
More Answers (2)
Walter Roberson on 27 Mar 2019
Loop backwards from length down to 1 so that the highest offset is assigned to first so it will not need to grow the array afterwards.