looping based on filename-length

3 次查看(过去 30 天)
Hi all,
I am working with images (>5000) and their date is in the filename. So when I want to know from what date the image originates, I use the filename.
example of a (blim)filename: '795348900.Thu.Mar.16_10_15_00.GMT.1995.nordzee1.cx.plan.bar'
so when I want to extract the year/month/day/time I use my function:
function [datenr] = get_time(blimfilename)
year = str2num(blimfilename(35:38));
month = blimfilename(15:17);
MonthName = ['Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct' 'Nov' 'Dec'];
MonthNum = strfind(MonthName, month); %gives ind corresponding to month (i.e. May = 5)
day = str2num(blimfilename(19:20));
hour = str2num(blimfilename(22:23));
min = str2num(blimfilename(25:26));
sec = str2num(blimfilename(28:29));
end
However, it turns out that from image nr 2370 onwards, the filename is 1 digit longer than the previous files to the previous code no longer works.
The (blim)filename is no matrix, but a single name every time (I wrote a loop). So I cannot create a loop saying if blimfilename = blimfilename(1:2369)... else ...
I was wondering if you know how to change my function that it does the lines above for a filename of 59 digits and the same lines but slightly different (see below) for a filename of 60 digits.
year = str2num(blimfilename(36:39));
month = blimfilename(16:18);
MonthNum = strfind(MonthName, month); %gives ind corresponding to month (i.e. May = 5)
day = str2num(blimfilename(20:21));
hour = str2num(blimfilename(23:24));
min = str2num(blimfilename(26:27));
sec = str2num(blimfilename(29:30));
Thank you in advance!
  3 个评论
Rik
Rik 2020-9-7
Another note: you should use str2double instead of str2num. The latter is using eval under the hood to get the value, so it can cause side effects if you have unsafe input. Since you don't check the file names they aren't guaranteed to be safe. I don't know any reason to use str2num in your own code (its replacement has existed for over 2 decades at least), and I assume the only reason Mathworks doesn't remove it is to allow backwards compatibility.
Nienke Vermeer
Nienke Vermeer 2020-9-7
Thank you both so much! This helped me a lot.

请先登录,再进行评论。

采纳的回答

Rik
Rik 2020-9-7
编辑:Rik 2020-9-7
You can use strsplit to split the file name in the chunks, as they seem to be delimited by points. Then you can select the appropriate cell element for the later variables.
If you are running pre-R2013a you may consider using regexp with the split option. If you're running pre-R2007b you should already be used to implementing your own solutions to common problems, but you may consider my getting my regexp_outkeys function from the FEX.
  1 个评论
Nienke Vermeer
Nienke Vermeer 2020-9-7
Thank you! I am running R2009, so I will indeed consider regexp with the split option.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by