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))
for i=1:length(v_in)
phase_in(i) = refprop(v_in(i),T_in,r410a);
end
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.

1 件のコメント

Stephen23
Stephen23 2019 年 3 月 27 日
編集済み: Stephen23 2019 年 3 月 27 日
By far the fastest and simplest an solution is to allocate the last element first, for example by simply looping over the indices backwards (as Walter Roberson explained in their answer). This topic has been discussed on the MATLAB blogs:
and also in many other Answers threads (with examples) e.g.:
Do NOT use a cell array of structures, this just makes your data storage more complex and less efficient, and means that you cannot use a simple comma-separated list for accessing the structure data:

サインインしてコメントする。

 採用された回答

KSSV
KSSV 2019 年 3 月 27 日

0 投票

S = struct ;
for i = 1:10
S(i).Rand = rand ;
S(i).loop = i ;
end

8 件のコメント

WANYI ZHANG
WANYI ZHANG 2019 年 3 月 27 日
I did not really understand on your answers.
Can you tell me what do I have to put in ''struct'',
is it just struct? or just my struct function name?
Thanks
WANYI ZHANG
WANYI ZHANG 2019 年 3 月 27 日
phase_in = struct;
for i=1:length(v_in)
phase_in(i) = refprop(v_in(i),T_in,r410a);
end
it gave me this error:
''Subscripted assignment between dissimilar structures.
Error in lab4_task3 (line 34)
phase_in(i) = refprop(v_in(i),T_in,r410a);''
KSSV
KSSV 2019 年 3 月 27 日
What is the ouput of this?
refprop(v_in(i),T_in,r410a)
WANYI ZHANG
WANYI ZHANG 2019 年 3 月 27 日
That is 1*100struct
KSSV
KSSV 2019 年 3 月 27 日
YOu better store them in a cell:
phase_in = cell(length(v_in),1);
for i=1:length(v_in)
phase_in{i} = refprop(v_in(i),T_in,r410a);
end
WANYI ZHANG
WANYI ZHANG 2019 年 3 月 27 日
編集済み: Walter Roberson 2019 年 3 月 27 日
That is very helpfull, it gave me as structure output.
Thank you so much.
Can I ask you one more quetion continue that.
I think it is better to put the code I write rather than expalining.
dm=zeros(length(v_in),1);
phase_in = cell(length(v_in),1);
fluid_up.P1= zeros(length(v_in),1);
fluid_up.P1= zeros(length(v_in),1);
for i=1:length(v_in)
phase_in{i} = refprop(v_in(i),T_in,r410a);
fluid_up(i).P1 = phase_in{i,1}.P; %underlined
fluid_up(i).rho = 1/phase_in{i,1}.vg; %underlined
dm(i) = orifice(fluid_up(i),P_d,orifice_params);
end
In matlab script these two bold variables still have underlying wave line,
How can I fix it.
Thanks a lot!
KSSV
KSSV 2019 年 3 月 27 日
dm=zeros(length(v_in),1);
phase_in = cell(length(v_in),1);
fluid_up = struct ;
for i=1:length(v_in)
phase_in{i} = refprop(v_in(i),T_in,r410a);
fluid_up(i).P1 = phase_in{i,1}.P;
fluid_up(i).rho = 1/phase_in{i,1}.vg;
dm(i) = orifice(fluid_up(i),P_d,orifice_params);
end
WANYI ZHANG
WANYI ZHANG 2019 年 3 月 27 日
Thanks a lot.
It is all worked.
I appreciate you.
Thanks again.

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2019 年 3 月 27 日

3 投票

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.

2 件のコメント

Stephen23
Stephen23 2019 年 3 月 27 日
編集済み: Stephen23 2019 年 3 月 27 日
Looping backwards is very easy:
for k = numel(v_in):-1:1 % backwards!!
S(k) = ... scalar structure
end
Just make sure that the output structure S does NOT exist in the workspace before the loop.
Walter Roberson
Walter Roberson 2019 年 3 月 27 日
(Or if it does exist, that it is a compatible structure and no larger than what will be needed.)

サインインしてコメントする。

Michael
Michael 2022 年 6 月 15 日
編集済み: Steven Lord 2023 年 7 月 7 日

0 投票

repmat offers another solution for preallocating structures:
[SL: Removed broken link to code generation documentation, added link to the main repmat function documentation page.]

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

製品

リリース

R2017b

質問済み:

2019 年 3 月 27 日

編集済み:

2023 年 7 月 7 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by