Reading specific range from multiple sheets in Excel

5 visualizaciones (últimos 30 días)
Andere
Andere el 12 de Dic. de 2016
Comentada: Andere el 13 de Dic. de 2016
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

Respuesta aceptada

dpb
dpb el 13 de Dic. de 2016
Editada: dpb el 13 de Dic. de 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 comentario
Andere
Andere el 13 de Dic. de 2016
Thank you dpb. Your answer has solved the problem. I accepted your answer and wish a pleasant day.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Import from MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by