I want to split cell vector

1 次查看(过去 30 天)
Hi everyone,
I have one cell vector with the date in this format {YY-MM-DD}, I would like to obtain three different vectors that contain 'YY', 'MM', 'DD'. I tried to convert this in str and using strsplit command but it doesn't work. Is there someone that can help me?? Thanks.
date =
1472×1 cell array
{'1985-01-01'}
{'1985-01-01'}
{'1985-01-01'}
{'1985-01-01'}
{'1985-01-02'}
{'1985-01-02'}
{'1985-01-02'}
{'1985-01-02'}
{'1985-01-03'}
{'1985-01-03'}

采纳的回答

Stephen23
Stephen23 2020-3-4
Using datetime:
>> DT = datetime(date,'InputFormat','yyyy-MM-dd');
>> DT.Year
ans =
1985
1985
1985
1985
1985
1985
1985
1985
1985
1985
>> DT.Month
ans =
1
1
1
1
1
1
1
1
1
1
>> DT.Day
ans =
1
1
1
1
2
2
2
2
3
3
Or using regexp:
>> tkn = regexp(date,'^(\d+)-(\d+)-(\d+)$','tokens','once');
>> tkn = vertcat(tkn{:});
>> Y = tkn(:,1)
Y =
'1985'
'1985'
'1985'
'1985'
'1985'
'1985'
'1985'
'1985'
'1985'
'1985'
>> M = tkn(:,2)
M =
'01'
'01'
'01'
'01'
'01'
'01'
'01'
'01'
'01'
'01'
>> D = tkn(:,3)
D =
'01'
'01'
'01'
'01'
'02'
'02'
'02'
'02'
'03'
'03'

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by