Dynamic Input File Name Recognition

조회 수: 1 (최근 30일)
Al Onen
Al Onen 2011년 9월 19일
Hello, I have many binary (*.grb) files to evaluate them in a function, but I want to call multiple (precisely 4) input files into the function from a directory. Files contain satellite data taken every 15 min period for the selected day. Here is an example name of an input binary file;
MSG2-SEVI-MSGMPEG-0100-0100-20100706130000.000000000Z-999896.grb
"MSG2-SEVI-MSGMPEG-0100-0100-"Part is same for all binaries;
Next "20100706" part is the date 07/06/2007 - Variable;
Next "13" is the hour of the day - Variable (from 00 to 23);
Next "00" is the minute of the hour of the day - Variable (00, 15, 30, 45);
Next "00" is second, and zero for all files ;
Next ".000000000Z-" part is again same for all files;
Next "999896" part is NOT same, but it has no use in functions;
Lastly ".grb" is the file extension as I expressed above.
What I want to do is to call all 15 minute period data (00,15,30,15) for a specific hour, in this case for the hour 13:00, and run them in the function. To do this I wrote the following wrong and insufficient code to call the files.
function MPEH(yr,mon,day,hr)
....
....
....
....
for i=1:4
min=['00';'15';'30';'45'];
In= strcat('MSG2-SEVI-MSGMPEG-0100-0100-20',yr,mon,day,hr,min(i),'00.000000000Z- and some code needed to ignore the rest');
Filename=strcat(In,'*.grb');
.....
.....
.....
What I need is to make Matlab ignore the same parts in the beginning and also the variable but not important part of the file names. As a summary recall of an example, When I type the following;
MPEH(10,07,06,13)
I need Matlab recognize and load the following files;
MSG2-SEVI-MSGMPEG-0100-0100- 201007061300 00.000000000Z-999896.grb
MSG2-SEVI-MSGMPEG-0100-0100- 201007061315 00.000000000Z-1010592.grb
MSG2-SEVI-MSGMPEG-0100-0100- 201007061330 00.000000000Z-999896.grb
MSG2-SEVI-MSGMPEG-0100-0100- 201007061345 00.000000000Z-1010592.grb
I know I failed to express my problem, Sorry for it, but I couldn't find a better way to describe it.
Many many thanks in advance.

채택된 답변

Al Onen
Al Onen 2011년 9월 20일
Considering the suggestions above, I typed the following code;
function [Data,Info]=MPEH(yr,mon,day,hr)
....
....
keymins={'00';'15';'30';'45'};
Directory = dir('*.grb');
....
....
for i=1:4
Pattern = strcat('MSG2-SEVI-MSGMPEG-0100-0100-20',yr,mon,day,hr,keymins{i});
Filename = regexp(Directory, Pattern, 'match');
....
....
But when I run the function, Matlab gives the following error code:
??? Undefined function or method 'regexp' for input arguments of type 'struct'.
Error in ==> MPEH at 37
Filename = regexp(Directory, Pattern, 'match');
I'm aware that my code is not correct. Can someone spare few more guidance?
Thanks in advance.
  댓글 수: 6
Walter Roberson
Walter Roberson 2011년 9월 22일
No no -- you should *display* the output of
cellfun(@class,{Directory.name},'Uniform',0)
and manually look for places that do not say 'char'.
Or use
find(~cellfun(@ischar,{Directory.name}))
and that will return the indices of the locations where Directory(K).name is _not_ a character array.
These are not things to put in to your regexp(), these are debugging steps to figure out why you were getting the regexp error.
However, considering what you are looking for, I would suggest not using regexp. Instead, after you have created Pattern, use
filematchinfo = Directory(strcmpn(Pattern, {Directory.name}, length(Pattern)));
and then filematchinfo will be a struct array in which the .name field will be the name of a matching file.
Or better yet, just go back to what you had originally and make the minor changes:
function MPEH(yr,mon,day,hr)
....
....
....
....
keymins=['00';'15';'30';'45'];
for i=1:4
Pattern= strcat('MSG2-SEVI-MSGMPEG-0100-0100-20',yr,mon,day,hr,min(i,:),'*.grb');
.....
.....
Directory = dir(Pattern);
and then after that, unless some stray files snuck in somehow,
Directory will be a single-element struct array and Directory.name will be the name of the one file.
Al Onen
Al Onen 2011년 9월 22일
Thanks, I returned to original one, you have helped greatly. I owe you a beer :)

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

추가 답변 (2개)

Nirmal Gunaseelan
Nirmal Gunaseelan 2011년 9월 19일
I'd strongly recommend using Regular Expressions for such parsing problems. It is fairly straight forward once you follow the examples in the doc page above.

Walter Roberson
Walter Roberson 2011년 9월 19일
A) Please do not name a variable "min", as that conflicts with the often-used routine min()
B) The place you involve min(i) should be min(i,:) if you are going to use character arrays of rows. I would probably use cell arrays instead, keymins = {'00', '15', '30', '45'} and then keymins{i} would be the string for a particular key minute representation.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by