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.
댓글 수: 4
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!