Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Extract Data from String

Subject: Extract Data from String

From: Greg Schubert

Date: 12 Aug, 2010 19:09:07

Message: 1 of 4

I need to extract data from a string. An example string array is below:

'il_mh_row1:9'
'il_mh_row1:10'

I need to extract all numbers after the :, and there may be 1 number or 3 numbers following the colon, depending on the case. I want to store these numbers in an array once I pull them out of the string. How do I do this? I have been looking over the regexp and sscanf functions all day with no luck.

Thanks

Subject: Extract Data from String

From: Walter Roberson

Date: 12 Aug, 2010 19:19:56

Message: 2 of 4

Greg Schubert wrote:
> I need to extract data from a string. An example string array is below:
>
> 'il_mh_row1:9'
> 'il_mh_row1:10'
>
> I need to extract all numbers after the :, and there may be 1 number or
> 3 numbers following the colon, depending on the case. I want to store
> these numbers in an array once I pull them out of the string. How do I
> do this? I have been looking over the regexp and sscanf functions all
> day with no luck.

sscanf(S, '%*[^:]:%f%f%f')

Subject: Extract Data from String

From: Andres

Date: 12 Aug, 2010 20:50:06

Message: 3 of 4

Walter Roberson <roberson@hushmail.com> wrote in message <i41hhm$do8$1@canopus.cc.umanitoba.ca>...
> Greg Schubert wrote:
> > I need to extract data from a string. An example string array is below:
> >
> > 'il_mh_row1:9'
> > 'il_mh_row1:10'
> >
> > I need to extract all numbers after the :, and there may be 1 number or
> > 3 numbers following the colon, depending on the case. I want to store
> > these numbers in an array once I pull them out of the string. How do I
> > do this? I have been looking over the regexp and sscanf functions all
> > day with no luck.
>
> sscanf(S, '%*[^:]:%f%f%f')

Hi Walter,
an elegant solution, but would you please drop a clue about the [^:] ("every character but the colon")? It's a regular expression, and does it work with sscanf, too? Is it documented? (I have no Matlab here to check.)
Thanks,
Andres

Subject: Extract Data from String

From: Walter Roberson

Date: 12 Aug, 2010 21:16:57

Message: 4 of 4

Andres wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message

>> sscanf(S, '%*[^:]:%f%f%f')

> Hi Walter,
> an elegant solution, but would you please drop a clue about the [^:]
> ("every character but the colon")?

Exactly. % introduces a format, * says to discard whatever is matched by the
format, [] indicates a character class, the ^ as the first character inside
the character class negates the rest of the class to mean "anything but", and
the colon is the character to be thus excluded.

> It's a regular expression, and does
> it work with sscanf, too?

Works with textscan() too.

> Is it documented? (I have no Matlab here to
> check.)

The sscanf() documentation doesn't mention negating the character class
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sscanf.html
but the documentation does defer to the C programming language documentation
and negated classes are part of sscanf() and fscanf() in C.

Tags for this Thread

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.

Contact us