How do I use 'textscan' to correctly read a formatted date?
Show older comments
I am trying to use the %{frmt}D syntax in textscan with a date, but am unable to get it to work, and instead observe unexpected behavior.
I have the following code. First, I define a date:
>> sTest = '[11-11-2016 17:08:53.453]; test~';
I then try match the %{frmt}D syntax to the date provided in 'sTest', and have the rest of it match to %q:
>> out1= textscan(sTest, '[%{MM-dd-yyyy HH:mm:ss.SSS}D]%q', 'Delimiter',{'~'});
Error using textscan
Unable to read the DATETIME data with the format 'MM-dd-yyyy HH:mm:ss.SSS'. If
the data is not a time, use %q to get text data.
>> out2 = textscan(sTest, '[%{MM-dd-yyyy HH:mm:ss.SSS}D]%q', 'Delimiter',{']','~'})
out2 =
1×2 cell array
[11-11-2016 17:08:53.453] {0×1 cell}
>> out3 = textscan(sTest, '[%{MM-dd-yyyy HH:mm:ss.SSS}D%q', 'Delimiter',{']','~'})
out3 =
1×2 cell array
[11-11-2016 17:08:53.453] {1×1 cell}
However, I get unexpected results, as seen above. The first case produces an error, the second does not pick up the second portion of the input, and the third case works, but I do not understand why. Can you explain what is happening?
Accepted Answer
More Answers (0)
Categories
Find more on Cell Arrays in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!