How do I add elements to a structure in a loop?

I'm trying to fit a number of polynomials to x/y, and want to save the results of each fit so I can evaluate which is best. So...
for norder = 2:2:highestOrder [p,S,mu] = polyfit(x,y,norder); end
I'd like a structure 'poly' to be able to store 'p', 'S', and 'mu' for each iteration of the for loop. Ideally the structure would look like poly.p2, to poly.phighestOrder, poly.S2...
How would I do that?

 채택된 답변

Stephen23
Stephen23 2018년 9월 20일

0 개 추천

Do not use a cell array of scalar structures, just use one non-scalar structure:
S = struct([]);
for k = 1:15
[S(k).a,S(k).b,S(k).c] = polyfit(randi(20,1,100),randi(10,1,100),2);
end
Giving all iterations in one structure.

추가 답변 (1개)

ANKUR KUMAR
ANKUR KUMAR 2018년 9월 20일

2 개 추천

Just simple store separated with dot. Example
for ii=1:15
[a,b,c]=polyfit(randi(20,1,100),randi(10,1,100),2)
poly{ii}.a=a;
poly{ii}.b=c;
poly{ii}.c=c;
end

댓글 수: 1

Kim
Kim 2018년 9월 20일
When I try that I get "Cell contents assignment to a non-cell array object." And for each iteration it just overwrites the previous.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

질문:

Kim
2018년 9월 20일

답변:

2018년 9월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by