open so many files with name of time series in order

조회 수: 1 (최근 30일)
Hamideh
Hamideh 2014년 7월 4일
댓글: dpb 2014년 7월 5일
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

답변 (2개)

dpb
dpb 2014년 7월 4일
편집: dpb 2014년 7월 5일
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 2014년 7월 5일
  댓글 수: 3
Image Analyst
Image Analyst 2014년 7월 5일
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 2014년 7월 5일
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...

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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by