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 Kommentare

Image Analyst
Image Analyst am 2 Aug. 2014
You forgot to attach ki2_out_acyc.txt so probably no one is going to do anything yet.
dpb
dpb am 2 Aug. 2014
Bearbeitet: dpb am 2 Aug. 2014
It seems otomh there could be ambiguous cases here, maybe? But perhaps they can be disambiguated by the files (presumably) being in sequence?
Anyway, just thinking at it, seems as you'll have to have a function that looks at the length of the string and inserts appropriately depending on that (and perhaps the preceding value). The case of three digits is, I think clearcut that for that case
>> d='410';
>> num2str(sscanf(d,'%1d').','%02d')
ans =
040100
>>
is a valid fixup; you'll need appropriate caveats on the cases for four and five digits in length.
Is there any chance you could simply regenerate the file(s) and fix the formatting there instead?
djr
djr am 2 Aug. 2014
Attached is the file. Dates are in the first column.
djr
djr am 2 Aug. 2014
I need the following format:
000410 for April 10th, 2000.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 2 Aug. 2014

0 Stimmen

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 Kommentar

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)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Andrei Bobrov
Andrei Bobrov am 2 Aug. 2014
Bearbeitet: Andrei Bobrov am 2 Aug. 2014

1 Stimme

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 am 2 Aug. 2014

0 Stimmen

Thanks guys... Both answers worked. Thanks so much!!!

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by