open so many files with name of time series in order

1 view (last 30 days)
Hello I have many text files with name of time series from 1998 until now with hourly time step. The name of files is like this: xmrg0101199800z (xmrgddmmyyyyhhz) xmrg0101199801z xmrg0101199802z . . xmrg0201199800z xmrg0201199801z . . xmrg0307201400z
xmrg and z are constant in the name of each file but hour, day, month and year change in order. I would like to open and import them open the through a subroutine. Can any one help me for this? Thanks Hamideh

Answers (2)

dpb
dpb on 4 Jul 2014
Edited: dpb on 5 Jul 2014
Try sotoo (caution, aircode, not tested)...
d=dir('xmrg??????????z.*'); % return the list of all
dd=strvcat(d(:).name); % make a character array of the names
dd=dd(:,5:14); % strip out the date string characters
dn=datenum(dd,'ddmmyyyyhh'); % turn to datenums
[~,ix]=sort(dn); % get sorted index
for i=1:length(d)
dat=importdata(d(ix(i)).name);
...
Of course, if these files were created on their namesake day and time, you could use the datenum value directly from the returned directory structure rather than from the file name itself. That presumes the datestamps haven't been munged on, of course.

Image Analyst
Image Analyst on 5 Jul 2014
  3 Comments
Image Analyst
Image Analyst on 5 Jul 2014
You're right. Most of the time the OS returns the files in alphanumeric order but it's not guaranteed so you do need to sort to make sure, either parse the date from the filename, or get it from the datenum field of the directory structure, both of which you pointed out. If the two dates don't agree, you might have to make a decision as to why. The FAQ code can then be used after the list of filenames has been sorted.
dpb
dpb on 5 Jul 2014
OK, just thought I'd note that since the FAQ doesn't address the order issue that perhaps you'd missed noticing that--I can't count the times I've somehow missed the obvious in a query...

Sign in to comment.

Categories

Find more on Dates and Time 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!