How to return structs properly

6 ビュー (過去 30 日間)
egal egal
egal egal 2017 年 11 月 13 日
コメント済み: egal egal 2017 年 11 月 15 日
Hi everyone! I got a question about how to properly handle returns of structs! I hope you can help me :-)
I did the following:
for i = 1:3
[maxStrain_at_maxForce(i)] = caclulate_strain(...);
end
This gives me following results:
Which I like very much. So now Matlab tells me for faster iteration, I should preallocate the variable maxStrain_at_maxForce. So i thought i will try with the following line:
maxStrain_at_maxForce = struct;
But that doesnt work. So I thought, okay I try this:
maxStrain_at_maxForce = struct('sample',[]);
Well of course I changed the for loop to:
for i = 1:3
[maxStrain_at_maxForce(i).sample] = caclulate_strain(...);
end
That gives me following result (I do understand why this happens)
The data is now stored in these maxStrain_at_maxForce(i).sample structs. My question now is how can I preallocate the struct, but still get the same result as without preallocating (first image)? I dont need this double structs.
I really appreciate your help! Thank you!
  2 件のコメント
James Tursa
James Tursa 2017 年 11 月 13 日
What are the dimensions of your actual problem? I can't imagine pre-allocating a 6 element struct would save you any significant amount of time.
egal egal
egal egal 2017 年 11 月 15 日
Depends on how many samples I test :-) not always the same.

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

採用された回答

James Tursa
James Tursa 2017 年 11 月 13 日
編集済み: James Tursa 2017 年 11 月 13 日
A couple of options for you:
% Run the loop in reverse. The first iteration pre-allocates the struct
for i = 3:-1:1
[maxStrain_at_maxForce(i)] = caclulate_strain(...);
end
maxStrain_at_maxForce = fliplr(maxStrain_at_maxForce); % Put back in correct order
or if you don't like that, then
maxStrain_at_maxForce(1) = caclulate_strain(...); % The first one
maxStrain_at_maxForce(3) = maxStrain_at_maxForce(1); % Pre-allocate the rest
for i = 2:3
[maxStrain_at_maxForce(i)] = caclulate_strain(...); % Fill in the rest
end
This all assumes, as does your current loop, that maxStrain_at_maxForce is not pre-existing with a different size or fields.
  1 件のコメント
egal egal
egal egal 2017 年 11 月 15 日
Okay, the first method seems to be a neat method :-)
So it seems there is no preallocation as it is for variables.

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

その他の回答 (1 件)

Rik
Rik 2017 年 11 月 13 日
You need to pre-allocate all fields of the struct.
maxStrain_at_maxForce = struct('eeff',zeros(6,1),'exy',zeros(6,1),...
'e1',zeros(6,1),'e2',zeros(6,1));
  2 件のコメント
Stephen23
Stephen23 2017 年 11 月 14 日
編集済み: Stephen23 2017 年 11 月 14 日
"You need to pre-allocate all fields of the struct"
Nope. This is pointless and possibly counterproductive.
As has been discussed before the structure header itself is stored as a contiguous array and can be preallocated for speed. But there is no point in preallocating the field arrays (unless each array is otherwise being enlarged on each iteration). If the field arrays are simply being allocated all at once (as in the example shown in the question) then there is no point in preallocating them. Preallocating the fields as well as the structure header will possibly just make things slower because of the time required to create all of those arrays... which are never even used! Waste of time.
This is explained in the MATLAB blogs:
is explained (rather obtusely) in the documentation:
and has been discussed many times before:
Yair Altman's blog also explains this:
Rik
Rik 2017 年 11 月 14 日
編集済み: Rik 2017 年 11 月 14 日
Thank you for the expansive comment. I guess you never stop learning.
(I think I came up with this mainly due to someone using getFrame and incorrectly trying to store that in a struct, which in that case resulted in fields mismatching)

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by