Read timestamp from data file with mixed formats

33 views (last 30 days)
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
dpb
dpb on 21 Apr 2015
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
per isakson
per isakson on 21 Apr 2015
"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.
dpb
dpb on 21 Apr 2015
Edited: dpb on 21 Apr 2015
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)

Peter Perkins
Peter Perkins on 22 Apr 2015
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
dpb
dpb on 22 Apr 2015
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.

Community Treasure Hunt

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

Start Hunting!