How to write a loop

Hi,
I have an excel file, I want to calculate the "average" of (A1,A2) and put in cell 1, (A3,A4) and put it in cell 2, (A5,A6) and put it in cell 3,... till end of the cell. Attached my excel file here.
and then write again in excel? how is it possible to write in Matlab?
Thanks

回答 (2 件)

Image Analyst
Image Analyst 2015 年 6 月 4 日

1 投票

Try something like this:
A = xlsread('rainfall.xlsx');
A2d = reshape(A, [length(A)/2, 2]);
means = mean(A2d, 2);
% Put in second column of workbook
xlswrite('rainfall.xlsx', means, 'B1');

3 件のコメント

SEPIDEH
SEPIDEH 2015 年 6 月 4 日
I got this error:
Warning: Could not start Excel server for export. XLSWRITE will attempt to write file in CSV format. > In xlswrite at 174 In prova1 at 5
So how should I solve this problem? thanks
Walter Roberson
Walter Roberson 2015 年 6 月 4 日
If you are on an MS Windows system, install Excel.
If you are not on an MS Windows system, then you will not be able to append on to the same worksheet, and you will instead need to create a new worksheet (that might include data copied from the original)
Image Analyst
Image Analyst 2015 年 6 月 4 日
I'm not sure if readtable() and writetable() require Excel - it doesn't say it does. They can read/write .xlsx format workbooks. .xlsx is an open source format so hopefully they don't need Excel. It's worth a try though. You need a fairly recent version of MATLAB though. Check the help to see it they are in your version.
Walter Roberson
Walter Roberson 2015 年 6 月 4 日

1 投票

YourData = xlsread('rainfall.xls');
n = length(YourData);
if mod(n,2) == 1; YourData(end+1) = 0; end; %0 pad to even rows
Result = mean(reshape(YourData, 2, []));
xlswrite('YourResult.xls',Result(:));

この質問は閉じられています。

タグ

質問済み:

2015 年 6 月 4 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by