From: Greg Schubert on
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
From: Walter Roberson on
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')
From: Andres on
Walter Roberson <roberson(a)hushmail.com> wrote in message <i41hhm$do8$1(a)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
From: Walter Roberson on
Andres wrote:
> Walter Roberson <roberson(a)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.