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 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