For loop to write in Excel file

Hi I have a problem with for loop and xlswrite statements. Actually I want to have a nested loop like this:
fid = fopen ('data1.xlsx', 'r');
fid = fopen ('reg1.txt', 'w');
for i = 1:4
x = data1(:,i);
for j = 2:5
y = data1(:,j);
p = polyfit(x,y,1);
yfit = polyval(p,x);
yresid = y - yfit;
ssresid = sum(yresid.^2);
A = ([i; j; ssresid]);
reg1 = xlswrite('reg.xlsx', [i, j, ssresid]);
end
end
and write the results of all iterations in Excel. However, I don't know how I can do this. The above code can just write the last iteration on the Excel file that it doesn't work for me. I want to have all iterations.
It would be great, If you can help me.
Thanks in advance

 採用された回答

Rafal Samborski
Rafal Samborski 2011 年 12 月 13 日

0 投票

This one should work:
fid = fopen ('data1.xlsx', 'r');
fid = fopen ('reg1.txt', 'w');
A = [];
for i = 1:4
x = data1(:,i);
for j = 2:5
y = data1(:,j);
p = polyfit(x,y,1);
yfit = polyval(p,x);
yresid = y - yfit;
ssresid = sum(yresid.^2);
A(:,end+1) = ([i; j; ssresid]);
end
end
reg1 = xlswrite('reg.xlsx', A);

1 件のコメント

Vahid
Vahid 2011 年 12 月 13 日
Thanks Rafal!
It works.
You did best.

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by