How to read data from one excel file and write it to another excel file
5 次查看(过去 30 天)
显示 更早的评论
As an example I have data in 1st excel file as follows:
Stage x y z
1 20.6 31.6 12.3
1 41.5 51.4 71.1
2 30.1 81.2 92.3
2 16.4 11.5 62.7
3 20.8 31.9 12.0
So I want to read all data in x column that refer to 'stage' '1', add them and then take out its average. Then write that calculated average to 2nd excel file wherein we write data(of 3 variables x,y and z) referring to the 'stage' column of 2nd excel document which data before executing this program as follows :
Stage x y z
1
1
2
2
3 and after execution as follows :
Stage x y z
1 31.05 41.5 41.7
1 31.05 41.5 41.7
2 23.25 46.35 77.5
2 23.25 46.35 77.5
3 20.8 31.9 12.0
Can anyone help me on this?
0 个评论
回答(2 个)
Megna Hari
2014-7-17
I'm not quite sure what you mean by x column of stage 1. Is stage 1: 20.6 31.6 12.3?
I would do blah=xlsread('filename.xls') and if your blah is set up like x=[1 20.6 31.6 12.3, 1 41.5 51.4 71.1, 2 30.1 81.2 92.3, 2 16.4 11.5 62.7, 3 20.8 31.9 12.0] like the way you wrote it then use [rows,cols,vals] = find(blah==1), ==2, ==3 etc. to find the columns where the stages start so you could reorganize them.
Unless your data is already set up in columns and not like the matrix I set up above?
Image Analyst
2014-7-17
Please attach your workbook so we can test. I think it would be something like
t = readtable(excelFullFileName);
stage = t.Stage; % Extract the "Stage" column.
rowsToExtract = stage == 1; % Find rows where stage = 1.
% Get output table.
tOutput = t(rowsToExtract); % Copy only stage=1 rows to output variable.
% Write out
writetable(tOutput, fullOutputFileName, 'WriteRowNames', true);
But I can't test it until you supply your file.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!