Finding array elements that include a specified combination of values?
1 次查看(过去 30 天)
显示 更早的评论
I have the following problem. I need to match two sets of dates in two separate arrays, but the formats are different (YYYYMM & YYYYMMDD), and in one of the arrays the dates are randomly mixed.
So I'd need a way to refer to specific parts of an element. As I have the correct timeline with YYYYMM dates, the easieast thing would be to somehow require the beginning of the YYYYMMDD element to match YYYYMM?
Thank you so much in advance!
0 个评论
采纳的回答
Andrew Newell
2011-4-23
You can extract the parts of the date using
yourDate = '201105';
switch length(yourDate)
case 6
[Y,M] = datevec(yourDate,'yyyymm')
case 8
[Y,M,D] = datevec(yourDate,'yyyymmdd')
otherwise
error('Invalid date string length.')
end
Y =
2011
M =
5
The same sequence with yourDate = '20110514' gives
Y =
2011
M =
5
D =
14
7 个评论
Andrew Newell
2011-5-1
Are you sure that your code works? What do you get for Y and M if you type [Y,M,D] = datevec('201112','yyyymmdd')?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!