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

1 次查看(过去 30 天)
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?

回答(1 个)

Cedric
Cedric 2013-11-1
编辑:Cedric 2013-11-5
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 个评论
Mike
Mike 2013-11-3
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 2013-11-5
编辑:Cedric 2013-11-5
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by