How to extract Jan and feb dates from an array of date strings?

조회 수: 2 (최근 30일)
JFz
JFz 2018년 7월 27일
댓글: JFz 2018년 8월 3일
Hi,
I have an array of cells of date string. I need to extract all the dates in Jan and Feb. How to do that? Thanks a lot!
  댓글 수: 3
JFz
JFz 2018년 7월 27일
The format in each sell is: 'mm/dd/yyyy'. some like: '1/1/2018' '2/1/2018' .... Thanks!
JFz
JFz 2018년 7월 27일
Something I created with {'1/1/2018'; '2/1/2018'; ...}

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2018년 7월 27일
>> C = {'1/1/2018'; '2/1/2018'; '12/31/2018'};
>> X = cellfun('isempty',regexp(C,'^[12]/','once'));
>> D = C(~X)
  댓글 수: 3
Stephen23
Stephen23 2018년 7월 27일
편집: Stephen23 2018년 7월 27일
"C = {'1/1/2018'; '2/1/2018'; '3/1/2018'; '4/1/2018'; '5/1/2018';'12/31/2018'}; And I would like to extract 1/2/2018 and 2/1/2018?"
Your example cell array does not contain '1/2/2018', should that example output actually be '1/1/2018' (which does exist in C) ? In which case my code already does that:
>> C = {'1/1/2018'; '2/1/2018'; '3/1/2018'; '4/1/2018'; '5/1/2018';'12/31/2018'};
>> X = cellfun('isempty',regexp(C,'^[12]/','once'));
>> D = C(~X)
D =
'1/1/2018'
'2/1/2018'
JFz
JFz 2018년 7월 30일
This works. Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Peter Perkins
Peter Perkins 2018년 8월 3일
If possible, don't do this with text. Convert to datetimes:
>> d = datetime(2018,randi(6,10,1),1)
d =
10×1 datetime array
01-Jan-2018
01-Mar-2018
01-Jan-2018
01-Mar-2018
01-Jan-2018
01-May-2018
01-Mar-2018
01-Jan-2018
01-Feb-2018
01-Mar-2018
>> d(d.Month==1 | d.Month==2)
ans =
5×1 datetime array
01-Jan-2018
01-Jan-2018
01-Jan-2018
01-Jan-2018
01-Feb-2018

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by