Filter löschen
Filter löschen

I want to split cell vector

1 Ansicht (letzte 30 Tage)
Valerio Gianforte
Valerio Gianforte am 4 Mär. 2020
Beantwortet: Stephen23 am 4 Mär. 2020
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'}

Akzeptierte Antwort

Stephen23
Stephen23 am 4 Mär. 2020
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'

Weitere Antworten (0)

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by