- When you give it the string '07' it identifies this as octal value 7 (which happens to be the same value as decimal 7).
- When you give it the string '08' it identifies that this is not a valid octal number, and so it returns zero and stops parsing the string.
Why am I getting inconsisant results with sscanf? I am expecting a result with five number. In the first two below I get five, in the second two I get three.
1 view (last 30 days)
Show older comments
>> sscanf('T=1998-191T07:50:00.000', 'T=%i-%iT%i:%i:%f')
ans =
1998
191
7
50
0
>> sscanf('T=1998-191T07:55:00.000', 'T=%i-%iT%i:%i:%f')
ans =
1998
191
7
55
0
>> sscanf('T=1998-191T08:00:00.000', 'T=%i-%iT%i:%i:%f')
ans =
1998
191
0
>> sscanf('T=1998-191T08:05:00.000', 'T=%i-%iT%i:%i:%f')
ans =
1998
191
0
>>
0 Comments
Accepted Answer
Stephen23
on 24 Feb 2018
Edited: Stephen23
on 24 Feb 2018
This happens because (as is clearly explained in the sscanf documentation) the format string '%i' detects the base of the number using the leading characters: a leading '0x' means it will read the number as hexadecimal, while a leading '0' will read the number as octal.
The solution is simple: do not use '%i', use '%u' instead (because '%u' always interprets the number as an unsigned integer with base 10), and always read the documentation carefully for every function and operation that you use, no matter how trivial it might seem:
>> sscanf('T=1998-191T08:50:00.000', 'T=%u-%uT%u:%u:%f')
ans =
1998
191
8
50
0
and note that you can easily add the day-of-the-year as a day of January:
>> V = datevec(datenum([1998,1,191,8,50,0]))
V =
1998 7 10 8 50 0
PS: you might like to download and try my FEX submission datenum8601, which converts an ISO 8601 date string (incl. week-number and ordinal syntaxes) into a serial date number:
>> N = datenum8601('T=1998-191T08:50:00.000')
N =
729946.368055556
>> V = datevec(N)
V =
1998 7 10 8 50 0
0 Comments
More Answers (0)
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!