Reading in xls files from folder- saving data with identifier from filename

1 view (last 30 days)
Hi, I have a folder full of xls files, named data_00001 through data_10000. Each file has a dozen or so identically named tabs full of RV's. I am interested in reading all files and tabs and creating histograms of the RV's.
Is there a way to read in the last 5 digits of the file names and attached them to each tab name (which I saved as a variable) ? Then each variable is a n*1 cell array, where each cell contains the raw RV data?
I used regexp to extract the number as a string and converted it to a double, and I used a for loop to save variable X {1,k} via importdata. How can I incorporate the saved double into this variable?

Answers (1)

Cedric
Cedric on 1 Nov 2013
Edited: Cedric on 5 Nov 2013
You can build a solution based on the following (not tested).
D = dir( 'Z:\MyFiles\FileName\d*.xls' ) ;
tabs = {'X', 'Y', 'Z'} ;
nFiles = length( D ) ;
nTabs = length( tabs ) ;
data = cell( nFiles, nTabs ) ;
for fId = 1 : nFiles
for tId = 1 : nTabs
idStr = D(fId).name(6:10) ;
data{fId,tId} = xlsread( D(fId).name, tabs{tId} ) ;
end
end
Let me know if your setup is different (in particular for tab names), and I can adapt the answer.
  2 Comments
Mike
Mike on 3 Nov 2013
Cedric- thanks for your post. I apologize for the confusion in my question, the tabs are not identically named in the sense of Results 1- n, but rather X,Y,Z... where each file has the same tabs.
I had a question - in D, can I replace '*.xls' with the path of the folder?ie... 'Z:\MyFiles\FileName\d*).xls' ?
Also, the For fID loop is as below? for fId = 1 : nFiles
Cedric
Cedric on 5 Nov 2013
Edited: Cedric on 5 Nov 2013
Hi Mike, you are right, the nFiles was truncated in one of my edits. See my updated answer and let me know if anything is unclear or doesn't apply to your case.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!