Reading specific range from multiple sheets in Excel

5 visualizzazioni (ultimi 30 giorni)
Hi there, Hope all is well. Ive been looking for a clear method on how to read a specific range for several Excel sheets. For example: I have this line:
T = xlsread ('myExcel.xlsx', 'sheet', 'N2:N30')
I want the 'sheet' to be from several sheets not only one. I have come across this code:
alldata = cell(1, m);
for(i=1:1:m); Sheet = char(sheetname(1,i));
alldata{i} = xlsread('test', Sheet);
end
But I could not get it. Can you please elaborate?
Thank you

Risposta accettata

dpb
dpb il 13 Dic 2016
Modificato: dpb il 13 Dic 2016
Use xlsfinfo to return the list of sheets in the workbook and iterate over the returned cellstr array..
excFile='myExcel.xlsx'; % Excel file of interest
rnge='N2:N30'; % desired range
[stat,sheets]=xlsfinfo(excFile); % get status, sheet(s) in workbook
if isempty(stat),error('bad Excel file format'),end % check status
for sht=sheets % iterate over 1xn array as column
T=xlsread(excFile,sht,rnge); % read each sheet, range in turn...
% Must do whatever with T here before reading next sheet...store or whatever
....
end
This will be slow because xlsread opens/closes the file every read operation. There's a FileExchange submittal that can do it without that overhead...but I don't have a direct link to it at hand, sorry.
  1 Commento
Andere
Andere il 13 Dic 2016
Thank you dpb. Your answer has solved the problem. I accepted your answer and wish a pleasant day.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Import from MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by