How to return structs properly

6 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by