How to process specific data from multiply Excel files?

I have around 30 Excel files that I want to process. From the second sheet of the excel files I want several cells imported into Matlab (AF18, AF20, AF16 and E25). The end results should be a matrix with the specific cells underneath each other (starting with datafile1 and so on). In the script I used xlsread, which for every new datafile I have to use new variables. Is there a way to process the data in a different way?
The files are not named datafile1, datafile2 and so on. The name is specific for each datafile.
%COP(Qc-Qe)(given)
filename = 'Datafile1.xlsx';
sheet = 2;
xlRange = 'AF18';
COP1 = xlsread(filename,sheet,xlRange);
% COP(dH comp)(given)
filename = 'Datafile1.xlsx';
sheet = 2;
xlRange = 'AF20';
COP2 = xlsread(filename,sheet,xlRange);
% COP(comp)= Qc/W_comp
filename = 'Datafile1.xlsx';
sheet = 2;
xlRange = 'AF16';
Qc = xlsread(filename,sheet,xlRange);
filename = 'Datafile1.xlsx';
sheet = 2;
xlRange = 'E25';
Wcomp = xlsread(filename,sheet,xlRange);
COP_comp = Qc/Wcomp;
% Create xls file
filename2 = 'COP_E3BUS_climatechamber_test.xlsx';
A = {'COP1', 'COP2', 'COPcomp'; COP1, COP2, COP_comp};
sheet = 1;
xlRange = 'A1';
xlswrite(filename2,A,sheet,xlRange);

回答 (1 件)

dpb
dpb 2017 年 3 月 6 日
編集済み: dpb 2017 年 3 月 6 日

0 投票

"...Is there a way to process the data in a different way?"
Of course! :)
First, either create a file containing the list of files or move them all to a working directory where they're the only .xls files so can just use d=dir('*.xlsx'); to retrieve the list to process or one of any other number of ways to isolate the group of files wanted rather than having to embed fixed names within the script/m-file.
Secondly, xlswrite is a real overhead bottleneck; you do not want to have to make multiple separate calls to it. My initial thought was to define a range name in the spreadsheet for the specific cells and use the name in the xlswrite call, but a test here shows only the first cell of a noncontiguous range is returned so that idea doesn't work.
So, the alternatives I see are to
  1. Read from upperleft:lowerright the range encompassing the area wanted and then just select those cells from that subsection for your purpose, or
  2. Use COM and write the proper Excel syntax to return the named range described above. I'm no Excel guru so no idea on that specific syntax, so I'd just do the first. For only 30-something files it'll be quick enough and straightforward to implement.
  3. Process the Excel files first outside Matlab and save the desired data to a more amenable file form to read into Matlab. Record a macro and execute it would be the logical way, I'd think, for this route.

質問済み:

2017 年 3 月 6 日

編集済み:

dpb
2017 年 3 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by