How to return structs properly
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
egal egal
am 13 Nov. 2017
Kommentiert: egal egal
am 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 Kommentare
James Tursa
am 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.
Akzeptierte Antwort
James Tursa
am 13 Nov. 2017
Bearbeitet: James Tursa
am 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.
Weitere Antworten (1)
Rik
am 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 Kommentare
Stephen23
am 14 Nov. 2017
Bearbeitet: Stephen23
am 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:
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!