I want to split cell vector

1 visualizzazione (ultimi 30 giorni)
Valerio Gianforte
Valerio Gianforte il 4 Mar 2020
Risposto: Stephen23 il 4 Mar 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'}

Risposta accettata

Stephen23
Stephen23 il 4 Mar 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'

Più risposte (0)

Categorie

Scopri di più su Introduction to Installation and Licensing in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by