I want to split cell vector

조회 수: 1 (최근 30일)
Valerio Gianforte
Valerio Gianforte 2020년 3월 4일
답변: Stephen23 2020년 3월 4일
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에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by