Iteratively selecting excel files

조회 수: 1 (최근 30일)
Muhammad Usman
Muhammad Usman 2014년 8월 15일
댓글: Muhammad Usman 2014년 8월 23일
Hi,i have around 1500 excel files in a folder with almost 900 x 130 cells of data in each file and i want to read (xlsread) one-by-one by randomly selected some rows from each file and this process continues to all files and then write (xlswrite) step by step to a new file. Please give some hint... Thanks

답변 (1개)

Image Analyst
Image Analyst 2014년 8월 16일
편집: Image Analyst 2014년 8월 16일
Geoff, he accepted it but I doubt he actually tried to code anything. If I were to do this, I'd use the FAQ http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F to loop over every xlsx file. Then use ActiveX calls to do the transfer of data:
myFolder = 'C:\my Excel workbooks';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.xls*');
% TO DO: ActiveX call to open Excel.
% TO DO: ActiveX call to open output workbook
excelFiles = dir(filePattern);
for k = 1:length(excelFiles)
baseFileName = excelFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% TO DO: ActiveX call to open workbook
% TO DO: ActiveX call to get number of rows
% TO DO: Call to randi(numberOfRows, 1,1) to get random starting and ending rows.
% TO DO: ActiveX call to read the data from the input workbook.
% TO DO: ActiveX calls to send data to output workbook.
% TO DO: ActiveX calls to close input workbook
end
% TO DO: ActiveX calls to save and close output workbook
% TO DO: ActiveX calls to quit Excel: Excel.Quit
% TO DO: delete Excel object: delete('Excel')
I attach an ActiveX demo that you will easily be able to adapt, or at least you should be able to.
The reason for using ActiveX, instead of xlsread() and xlswrite(), is that you want to process these 1500 files in a finite amount of time. If you don't use ActiveX it could take hours instead of minutes.
Also, I don't know what is interactive about this. You said you want to process them one by one. If you don't want all of them in the folder but just user-chosen ones, then skip the call to dir() and put a call to uigetfile() inside the loop, though with 1500 files there is no way on earth your use will remember which files have been processed yet and which have not, and thus will not know which file to select.
  댓글 수: 3
Image Analyst
Image Analyst 2014년 8월 23일
Muhammad Usman
Muhammad Usman 2014년 8월 23일
yes i am sorry for that. Please help me on my this code.

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

카테고리

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