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 件のコメント

Stephen23
Stephen23 2020 年 9 月 7 日
編集済み: Stephen23 2020 年 9 月 7 日
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
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 日

0 投票

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 件)

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by