Borrar filtros
Borrar filtros

How to sequence series of excel files and extract certain rows:columns from each file automatically?

1 visualización (últimos 30 días)
There are a series of excel files from 1 to 99, from file01.xlsx to file99.xlsx. I only want cells B1:C3 from each excel file. I used this code:
Efiles = dir('*.xlsx');
numfiles = length(Efiles);
mydata = cell(1,numfiles);
for k = 1:numfiles
myfilename = sprintf('file%d.xlsx',k);
mydata{k} = xlsread(Efiles(k).name);
end
Unfortunately, this extracts all the data from the excel file. I do not know how to utilize for loops to get the desired B1:C3 cells from each excel file.
Additionally, the workspace shows the extracted data as a cell array. How do you convert the cell array to regular array (matrix)? Otherwise, I can't use the data.
Thank you. (Sorry, MatLab beginner here, searched hours for answers, but struggling a lot).
<http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F > does NOT work in my case.

Respuesta aceptada

KSSV
KSSV el 26 de Sept. de 2018
files = dir('*.xlsx') ; % GEt all excel files in the folder
N = length(files) ; % total number of files
iwant = cell(N,1) ;
for i = 1:N
iwant{i} = xlsread(files(i).name,'B1:C3');
end
  2 comentarios
John Gow
John Gow el 26 de Sept. de 2018
Thank you! It worked well, but how would I do this if I want to extract different cells within in Excel and import them?
For example, instead of B1:C3, I want A1:A3 and C1:C3.
By putting it in the iwant{i} line?
Image Analyst
Image Analyst el 26 de Sept. de 2018
Just pass it in to xlsread(). You can make up a string with sprintf() if you want:
cellReference = sprintf('A%d:C%d', row1, row2);
iwant{i} = xlsread(files(i).name, cellReference );

Iniciar sesión para comentar.

Más respuestas (1)

John Gow
John Gow el 26 de Sept. de 2018
Thank you! I just relooped it.

Community Treasure Hunt

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

Start Hunting!

Translated by