How to return structs properly

6 vues (au cours des 30 derniers jours)
egal egal
egal egal le 13 Nov 2017
Commenté : egal egal le 15 Nov 2017
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 commentaires
James Tursa
James Tursa le 13 Nov 2017
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 le 15 Nov 2017
Depends on how many samples I test :-) not always the same.

Connectez-vous pour commenter.

Réponse acceptée

James Tursa
James Tursa le 13 Nov 2017
Modifié(e) : James Tursa le 13 Nov 2017
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 commentaire
egal egal
egal egal le 15 Nov 2017
Okay, the first method seems to be a neat method :-)
So it seems there is no preallocation as it is for variables.

Connectez-vous pour commenter.

Plus de réponses (1)

Rik
Rik le 13 Nov 2017
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 commentaires
Stephen23
Stephen23 le 14 Nov 2017
Modifié(e) : Stephen23 le 14 Nov 2017
"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 le 14 Nov 2017
Modifié(e) : Rik le 14 Nov 2017
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)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by