Read timestamp from data file with mixed formats

I have a file with mixed date:
23.4 F15 2014-106T19:11:46
22.1 F16 2015-203T00:25:26
13.3 F100 2015-250T20:16:50
.
.
I know how to read the first two columns but I can't figure out how to scan the date and time from the third column. I tried:
formatSpec = '%f%s %{yyyy-DDDTHH:mm:ss}D';
delimiter = {'\t',' '};
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter,...
'MultipleDelimsAsOne', true, 'ReturnOnError', false);
but I get the error 'Badly formed format string'
Does anyone know what format can be used for the third column? Thanks.

3 Comments

Your |formatSpec | looks okay to me. Which Matlab release do you use? R2014b needed.
The issue is as noted in the Answer previously posted that you didn't enclose the non-recognized ASCII character inside the date format string in single quotes to escape it from the rest of the format string.

Sign in to comment.

 Accepted Answer

dpb
dpb on 20 Apr 2015
Edited: dpb on 21 Apr 2015
From
doc datetime % format descriptor--
"... To include the letters A-Z and a-z as literal characters in the format, enclose them with single quotes."
So, yours should be
formatSpec = '%f%s %{yyyy-DDD''T''HH:mm:ss}D';
NB: the doubled-up single quotes to embed one in the final string.

3 Comments

Thanks for the reply. I tried this but I still get the "Badly formed format string" error. Did this work on your MATLAB setup? I tried making a separate file with just one line:
2014-106T19:11:46
and used the format
formatSpec = '%{yyyy-DDD''T''HH:mm:ss}D';
with the textscan(fileID, formatSpec); If you could read the data then it's probably an issue with my MATLAB version. I am running R2014a on ubuntu. I will do a character counting to get around it if this formatting doesn't work.
"I am running R2014a" &nbsp Are you sure %D is supported? Release note of R2014b says
Import of data as categorical and datetime arrays using the readtable
and textscan functions
The readtable and textscan functions can read data from text files a
categorical or datetime arrays. Use the %C conversion specifier to read
text as a category name. Use the %D conversion specifier to read text as
a datetime value.
I'm on R2012b which doesn't support it at all so I can't test it, sorry.
The doc is from R2014b so you'll have to check re: 2014a but looks like it's not implemented is best guess. I'd probably revert to reading the fields as numeric and then call datetime with the array...
>> s='2014-106T19:11:46';
>> cell2mat(textscan(s,'%4d-%3dT%2d:%2d:%2d','collectoutput',1))
ans =
2014 106 19 11 46
>>
ADDENDUM
Another thought just popped into my head...read the field as another string then simply delete the 'T' from the array of strings. Then, presuming it's only the extension of the embedded character that's failing in your release you can use the %D format on that cleaned-up array.

Sign in to comment.

More Answers (1)

Hadi, in R2014b, you have the right format string for %D. But as Per points out, you need R2014b to use %D. In R2014a, you can read them as strings. But you have day-of-year in the strings, and the older datenum/datestr/datevec functions don't support that, so I think you're going to have to parse the separate pieces as numbers, and then put them together using datevec.

1 Comment

Version issue aside, the doc gives an example using an embedded 'T' in the %D string and specifically notes it (the T or any other non-specific date character must be a single-quoted string???? (The point being the string as Hadi initially posted isn't formatted properly as I read the doc, anyway, or by the example)

Sign in to comment.

Categories

Asked:

on 20 Apr 2015

Commented:

dpb
on 22 Apr 2015

Community Treasure Hunt

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

Start Hunting!