필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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.

채택된 답변

KSSV
KSSV 2018년 9월 26일
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
John Gow
John Gow 2018년 9월 26일
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 2018년 9월 26일
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 );

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

John Gow
John Gow 2018년 9월 26일
Thank you! I just relooped it.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by