How to write .xls file to include every output from a for loop?

1 回表示 (過去 30 日間)
car
car 2018 年 3 月 20 日
コメント済み: car 2018 年 3 月 21 日
So I have about 50 excel files that I've written a script to loop through to apply several functions, one of which is a polyfit function.
for k=1:length(source_files)
[rows, columns] = size(numData);
year=numData(1:rows,1);
maxt=numData(1:rows,3);
JanMaxT = maxt(1:12:end);
JanYear = year(1:12:end);
idxValid = ~isnan(JanMaxT);
fitJanMaxT = polyfit(JanYear(idxValid),JanMaxT(idxValid),1);
end
now I want to write fitJanMaxT to an excel file which includes all 50 of the polyfit outputs using xlswrite, but when I do I only get the polyfit value of the last of the 50 original files. In other words, the xlswrite function overwrites after each iteration of the for loop. How can I make it so the xlswrite function adds a new row of polyfit data for each input file I have? Also, can I add a column to the newly written xls file that has the names of each of my original 50 files (souce_files)?
Thanks fo the input!!

採用された回答

Walter Roberson
Walter Roberson 2018 年 3 月 20 日
編集済み: Walter Roberson 2018 年 3 月 20 日
for k=1:length(source_files)
thisfile = source_files{k};
numData = xlsread(thisfile);
[rows, columns] = size(numData);
year=numData(1:rows,1);
maxt=numData(1:rows,3);
JanMaxT = maxt(1:12:end);
JanYear = year(1:12:end);
idxValid = ~isnan(JanMaxT);
fitJanMaxT(k,:) = polyfit(JanYear(idxValid),JanMaxT(idxValid),1);
end
datacell = [source_files(:), num2cell(fitJanMaxT)];
xlswrite('YourOutput.xls', datacell)
  1 件のコメント
car
car 2018 年 3 月 21 日
beautiful. works perfectly. Thanks!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by