Extract the date from a standard filename

10 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Dates and Time 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by