How does sscanf work?

3 views (last 30 days)
MINA on 1 Apr 2017
Commented: Walter Roberson on 1 Apr 2017
Can someone please tell me what does this line means?
elec=sscanf(str, '%*[TT]%d%');

Answers (1)

Walter Roberson
Walter Roberson on 1 Apr 2017
That code is almost certainly wrong. It should probably be
str = 'TT1.sp.mat';
elec = sscanf(str, 'TT%d');
Walter Roberson
Walter Roberson on 1 Apr 2017
To know exactly you would need to get a job with Mathworks.
You can view C source for a standard version at
This does not support the %D date formats or %q quoted strings or complex numbers and has nothing about how arrays are represented in MATLAB.
If you were just looking for a brief guide to using sscanf format strings:
sscanf recognizes three elements in strings: escape codes, format descriptors, and literal characters.
Escape codes always begin with \ and are continued for the next few characters. They give an easy way to refer to characters such as tab and newline or to bytes by their numeric code. For example if you wanted to indicate that you expect a "ë" at that point you can do that with \xEB. Any character designated this way must appear in the input stream, and if it does then it is discarded and processing continues. If it is not there then sscanf terminates parsing and returns what it got to that point.
Format descriptions always begin with % and continue for several characters. More about those in a moment.
Any character that is not an escape sequence or a format description must appear in the input stream at that point, same as for escape sequence processing.
The % format descriptions encode what kind of number or string is expected in the input at that point. Whitespace (blanks) are silently discarded until nonblank is found and then input conversion begins. The routine then keeps reading characters as long as the characters are consistent with the requested kind of number, and when a character that is not consistent is found then the character is "put back" and numeric conversion is done on what was accumulated.
So TT%d means to read a character and make sure it is a T and discard it, then read another character and make sure it is a T and discard it, and then to read as many characters as possible that can form part of an integer (%d requests integer). That pulls in the 1 and then the period and sees the period is not allowed in an integer so it puts back the period and does numeric conversion on the '1' it accumulated. Then it goes back to the beginning of the format string because it reached the end with more input available. It looks for a T but finds the put back period instead so it terminates the call and returns the numeric 1 that had been read in.

Sign in to comment.


Find more on Characters and Strings 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!