How to read data from one excel file and write it to another excel file
5 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Megna Hari
on 17 Jul 2014
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
on 17 Jul 2014
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.
4 Comments
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!