Read timestamp from data file with mixed formats

16 次查看(过去 30 天)
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 个评论
dpb
dpb 2015-4-21
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.

请先登录,再进行评论。

采纳的回答

dpb
dpb 2015-4-20
编辑:dpb 2015-4-21
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 个评论
per isakson
per isakson 2015-4-21
"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 2015-4-21
编辑:dpb 2015-4-21
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.

请先登录,再进行评论。

更多回答(1 个)

Peter Perkins
Peter Perkins 2015-4-22
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 个评论
dpb
dpb 2015-4-22
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)

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by