transforming two date vectors

조회 수: 1 (최근 30일)
Sabbas
Sabbas 2012년 8월 6일
Dear all,
I have
A={
'2/11/2008'
'30/11/08'
'28/12/08'
'25/01/09'
'22/02/09'
'22/03/09'
'19/04/09'
'17/05/09'
'14/06/09'
'12/7/2009'
'9/8/2009'
'6/9/2009'
'4/10/2009'
'1/11/2009'
'29/11/09'
'27/12/09'
'31/01/10'
'28/02/10'
'28/03/10'
'25/04/10'
'23/05/10'
'20/06/10'
'18/07/10'
'15/08/10'
'12/9/2010'}
and
B={
'1/4/2009'
'2/1/2009'
'3/1/2009'
'4/5/2009'
'5/3/2009'
'5/31/2009'
'7/5/2009'
'8/2/2009'
'8/30/2009'
'10/4/2009'
'11/1/2009'
'11/29/2009'
'1/3/2010'
'1/31/2010'
'2/28/2010'
'4/4/2010'
'5/2/2010'
'5/30/2010'
'7/4/2010'
'8/1/2010'
'8/29/2010'
'10/3/2010'
'10/31/2010'
'11/28/2010'
'1/2/2011'
'1/30/2011'
'2/27/2011'
'4/3/2011'
'5/1/2011'
'5/29/2011'
'7/3/2011'
'7/31/2011'
'8/28/2011'
'10/2/2011'
'10/30/2011'
'11/27/2011'
'1/4/2009'
'2/1/2009'}
Sometimes I have either vector A or vector B. I want to find a unified code that will convert these vectors to the format dd/mm/yy irrespective of whether I have A or B
thanks
  댓글 수: 3
Sabbas
Sabbas 2012년 8월 6일
B has a characteristic. you get dates of the format '10/31/2010' So you can have mm/dd/yyyy whereas in the first vector you do not have such cases. I suppose you can use this to "know" when you have vector A or vector B
thanks
Matt Fig
Matt Fig 2012년 8월 7일
So you can guarantee that every set of dates will have at least one day D where D>12? You did not specify that in the original statement, but it is good to know!

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 8월 7일
a = regexp(DateInput,'(^\d*)(?=/)','match');
if any(str2double(cat(1,a{:}))>12)
dfmt = 'dd/mm/yyyy';
else
dfmt = 'mm/dd/yyyy';
end

추가 답변 (1개)

per isakson
per isakson 2012년 8월 6일
편집: per isakson 2012년 8월 6일
Try
cac = cellfun( @(str) transpose( sscanf( str, '%d/%d/%*d' ) ) ...
, A, 'uni', false );
num = cell2mat( cac );
isd = any( num >= 13, 1 );
if all( not( isd ) )
msg = 'Cannot decide';
elseif all( isd )
msg = 'Illegal date';
elseif isd(1)
msg = 'day first';
elseif isd(2)
msg = 'month first';
else
msg = 'Error in code';
end
  댓글 수: 1
per isakson
per isakson 2012년 8월 6일
To avoid the long line I edited and introduced an error, which is now fixed.

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

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by