Extract the date from a standard filename

13 ビュー (過去 30 日間)
James hall
James hall 2013 年 4 月 2 日
Hi Guys,
I have thousands of .MAT files which hold data with the file name convention
Vehicle_Registration_Type_Date.MAT
the Type is exactly the same for every file.
Currently I am using
DateLocation=regexp(SelectedMat,'\d');
DateVals=regexp(SelectedMat,'\d','match');
DateVal = cell2mat(DateVals);
DateNum = datenum(DateVal,'yymmdd');
DateStr = datestr(DateNum, 'dd-mm-yy');
I am finding that the 2 digits in the Registration are appearing in the datestring making it wrong.
e.g Vehicle_XX62XXX_Type_130101 returns 62130101
The date is in US format yymmdd although I am in the UK
is there a way to filter the string to only be searched after type?
Any help would be greatly appreciated
Many Thanks
James

採用された回答

Jan
Jan 2013 年 4 月 2 日
Str = 'Vehicle_XX62XXX_Type_130101';
index = strfind(Str, '_');
S = Str(index(end):end);
StrDate = [S(5:6), '-', S(3:4), '-', S(1:2)];
This is a really stupid approach, without 2 REGEXPs, CELL2MAT, DATENUM and DATESTR, which are all such powerful, that they need a lot of time.
  1 件のコメント
James hall
James hall 2013 年 4 月 2 日
Hi Jan,
This worked well after one correction
Str = 'Vehicle_XX62XXX_Type_130101';
index = strfind(Str, '_');
S = Str(index(end):end);
% StrDate = [S(5:6), '-', S(3:4), '-', S(1:2)];
StrDate = [S(6:7), '-', S(4:5), '-', S(2:3)];
I found that it was including a underscore into StrDate without.
I'm assuming you meant that the code was stupid, can fully accept that I may have been stupid using so many highpower functions to gain the same answer :D. Either way many thanks for the Help
James

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTime Series Objects についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by