looping based on filename-length

3 views (last 30 days)
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 Comments
Rik
Rik on 7 Sep 2020
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 on 7 Sep 2020
Thank you both so much! This helped me a lot.

Sign in to comment.

Accepted Answer

Rik
Rik on 7 Sep 2020
Edited: Rik on 7 Sep 2020
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 Comment
Nienke Vermeer
Nienke Vermeer on 7 Sep 2020
Thank you! I am running R2009, so I will indeed consider regexp with the split option.

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings 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!