Thread Subject: sscanf

Subject: sscanf

From: doe john

Date: 27 Oct, 2008 13:46:01

Message: 1 of 3

HI, I have a chain of characters string + number delimited by semi '; ' as following

line ='name.rrr; ; 78;7;6;;string;;0'

first I replacethe ';' by a tab
line = regexprep(line,';','\t')
Then, I try to use
[A, count, errmsg, nextindex]=sscanf(line, '%s, %d,%d,%d,%d,%s,%s,%s,%d')
but I was not able to retrieve those different data. I get the name.rr in A but in error , I get matching failure in format. I don't understand why it doesnt retrieve other data.


  Thanks

  Joe

Subject: sscanf

From: Walter Roberson

Date: 27 Oct, 2008 15:00:28

Message: 2 of 3

doe john wrote:
> HI, I have a chain of characters string + number delimited by semi '; ' as following
 
> line ='name.rrr; ; 78;7;6;;string;;0'

> first I replacethe ';' by a tab
> line = regexprep(line,';','\t')
> Then, I try to use
> [A, count, errmsg, nextindex]=sscanf(line, '%s, %d,%d,%d,%d,%s,%s,%s,%d')
> but I was not able to retrieve those different data. I get the name.rr in A but in error ,
> I get matching failure in format. I don't understand why it doesnt retrieve other data.

When you put a comma in your sscanf format, then sscanf will look for a literal comma in
your input. Do not put any delimiter between the various format elements. A lot of people
put spaces, but it is best not to even put those. For example, you might use

'%s%d%d%d%d%s%s%s%d'

However, you are still going to have problems with the empty fields. Both of the
format elements you have specified, %s and %d, skip -all- leading whitespace, where
"whitespace" is tab or space (or vertical tab, or carriage return, or newline.)
The leading %s format will read the 'name.rrr', but the %d after that will skip over
to the 78, the second %d will get the 7, the third %d will get the 6, then the
fourth %d will skip over and try reading from 'string', which is going to fail.
And if it did somehow work (e.g., the string happened to be all digits), then
the %s that follows in the format string would skip over the whitespace to the 0
leaving you with a %s and %d not satisfied.

I would suggest that for your purposes, it would probably be easier to split
at the semi-colon and parse the individual parts.

Subject: sscanf

From: Andres

Date: 27 Oct, 2008 15:02:02

Message: 3 of 3

"doe john" <johnnydoe1515@gmail.com> wrote in message <ge4gmp$r05$1@fred.mathworks.com>...
> HI, I have a chain of characters string + number delimited by semi '; ' as following
>
> line ='name.rrr; ; 78;7;6;;string;;0'
>
> first I replacethe ';' by a tab
> line = regexprep(line,';','\t')
> Then, I try to use
> [A, count, errmsg, nextindex]=sscanf(line, '%s, %d,%d,%d,%d,%s,%s,%s,%d')
> but I was not able to retrieve those different data. I get the name.rr in A but in error , I get matching failure in format. I don't understand why it doesnt retrieve other data.

sscanf reads the first string, but then expects a comma as it appears after the first %s in your format string.

Besides, reading mixed strings and numbers is not very comfortable with sscanf since it will put everything together into a single numeric matrix (read the "Output Characteristics: Both Numeric and Character Values Read" paragraph in the sscanf doc).


Matlab's textscan is much better here:

line ='name.rrr; ; 78;7;6;;string;;0'
A = textscan(line,'%s %d %d %d %d %s %s %s %d','Delimiter',';');


(it should definitely be listed in the "See also" section of the sscanf help page, too)

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
sscanf Andres 27 Oct, 2008 11:05:07
textscan Andres 27 Oct, 2008 11:05:07
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com