looping based on filename-length
Mostrar comentarios más antiguos
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 comentarios
I doubt that this code comment is correct:
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)
The square brackets are a concatenation operator, so this
['Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct' 'Nov' 'Dec']
is exactly equivalent to this:
'JanFebMarAprMayJunJulAugSepOctNovDec'
amd then strfind will return the start index, e,g, for 'May' this will return 13. Lets try it and see:
>> MonthName = ['Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct' 'Nov' 'Dec']
MonthName = JanFebMarAprMayJunJulAugSepOctNovDec
>> MonthNum = strfind(MonthName, 'May')
MonthNum = 13
A working approach would use a cell array/string array and strcmpi (or one of that family):
>> MonthName = {'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'};
>> MonthNum = find(strcmpi(MonthName, 'May'))
MonthNum = 5
Rik
el 7 de Sept. de 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
el 7 de Sept. de 2020
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!