Add zeros to some dates

조회 수: 6 (최근 30일)
djr
djr 2014년 8월 2일
편집: Andrei Bobrov 2014년 8월 2일
Hi,
I have a file with dates that are in the format 'yymmdd'. For example, 991231 would be Dec 31st, 1999. However when it gets into 2000, the dates in the file are missing zeros before the first non-zero number. For instance, 410 stands for April 10th, 2000. Therefore, I have problems to cnevert it into the Matlab date format.
The file is in txt format so I opened it like this:
fid = fopen('ki2_out_acyc.txt');
acyc = textscan(fid, '%s');
fclose(fid);
The following functions do not work because it cannot recognize dates that are missing zeros:
ACdate = cellfun(@(x) datenum(x,'yymmdd'), acyc)
ACdata = datenum(acyc{1}, 'yymmdd')
ACdate = datenum(acyc{1})
I guess I have to add zeros to the dates that lack zeros. I have very large set of data so I cannot do it manualy.
Please could you help me with this? Thanks, Djordje
  댓글 수: 4
djr
djr 2014년 8월 2일
Attached is the file. Dates are in the first column.
djr
djr 2014년 8월 2일
I need the following format:
000410 for April 10th, 2000.

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 2일
fid = fopen('file.txt');
acyc = textscan(fid, '%s');
fclose(fid);
w=cellfun(@(x) [repmat('0',1,6-numel(x)) x],acyc{:},'un',0)
ACdate = cellfun(@(x) datenum(x,'yymmdd'),w)
  댓글 수: 1
Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 2일
acyc=dlmread('ki2_out_acyc.txt')
d=arrayfun(@num2str,acyc(:,1),'un',0)
w=cellfun(@(x) [repmat('0',1,6-numel(x)) x],d,'un',0)
ACdate = cellfun(@(x) datenum(x,'yymmdd'),w)

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

추가 답변 (2개)

Andrei Bobrov
Andrei Bobrov 2014년 8월 2일
편집: Andrei Bobrov 2014년 8월 2일
out = datenum(cellfun(@(x)sprintf('%06s',x),acyc{:},'un',0),'yymmdd');
right variant
f = fopen('ki2_out_acyc.txt');
c = textscan(f,'%s %d %d %f %f %f %f %f','CollectOutput',1);
fclose(f);
out = datenum(cellfun(@(x)sprintf('%06s',x),c{1},'un',0),'yymmdd');
a1 = (out - now) > 0;
out(a1) = arrayfun(@(x)addtodate(x,-100,'year'),out(a1));

djr
djr 2014년 8월 2일
Thanks guys... Both answers worked. Thanks so much!!!

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by