フィルターのクリア

Dynamic Input File Name Recognition

2 ビュー (過去 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 Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by