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:
How do I extract data from a string

Subject: How do I extract data from a string

From: Jeff

Date: 7 Mar, 2010 04:03:21

Message: 1 of 7

Can someone write some quick code which shows me how to extract the R value from strings in Matlab? The R value is between the R and the next underscore (_). Here are two example strings:

'lorenzData_R0.9_z26.499398169954013.csv'
'lorenzData_R1.1_z26.499398169954013.csv'
'lorenzData_R20_z26.499398169954013.csv'

So I need the 0.9, 1.1, and 20. And I can't figure out how to do it in Matlab (regexpressions might help, but I've never heard of them and Matlab's help is not enough to get me up to speed).

Thanks,

Subject: How do I extract data from a string

From: dpb

Date: 7 Mar, 2010 05:23:15

Message: 2 of 7

Jeff wrote:
> Can someone write some quick code which shows me how to extract the R
> value from strings in Matlab? The R value is between the R and the next
> underscore (_). Here are two example strings:
>
> 'lorenzData_R0.9_z26.499398169954013.csv'
> 'lorenzData_R1.1_z26.499398169954013.csv'
> 'lorenzData_R20_z26.499398169954013.csv'
>
> So I need the 0.9, 1.1, and 20. And I can't figure out how to do it in
> Matlab (regexpressions might help, but I've never heard of them and
> Matlab's help is not enough to get me up to speed).

 >> s='lorenzData_R0.9_z26.499398169954013.csv'
s =
lorenzData_R0.9_z26.499398169954013.csv
 >> v = sscanf(s,'lorenzData_R%f')
v =
     0.9000
 >>

--

Subject: How do I extract data from a string

From: Jonathan

Date: 7 Mar, 2010 05:34:06

Message: 3 of 7

"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hmv8i9$oc4$1@fred.mathworks.com>...
> Can someone write some quick code which shows me how to extract the R value from strings in Matlab? The R value is between the R and the next underscore (_). Here are two example strings:
>
> 'lorenzData_R0.9_z26.499398169954013.csv'
> 'lorenzData_R1.1_z26.499398169954013.csv'
> 'lorenzData_R20_z26.499398169954013.csv'
>
> So I need the 0.9, 1.1, and 20. And I can't figure out how to do it in Matlab (regexpressions might help, but I've never heard of them and Matlab's help is not enough to get me up to speed).
>
> Thanks,

 num = regexp(inputString,'(?<=_R)[\d.]+(?=_)','match');

of if you need an actual numeric values

num = num2str(regexp(inputString,'(?<=_R)[\d.]+(?=_)','match'));

-Jonathan

Subject: How do I extract data from a string

From: Jeff

Date: 7 Mar, 2010 08:55:23

Message: 4 of 7

dpb <none@non.net> wrote in message <hmvdaj$e7r$1@news.eternal-september.org>...
> Jeff wrote:
> > Can someone write some quick code which shows me how to extract the R
> > value from strings in Matlab? The R value is between the R and the next
> > underscore (_). Here are two example strings:
> >
> > 'lorenzData_R0.9_z26.499398169954013.csv'
> > 'lorenzData_R1.1_z26.499398169954013.csv'
> > 'lorenzData_R20_z26.499398169954013.csv'
> >
> > So I need the 0.9, 1.1, and 20. And I can't figure out how to do it in
> > Matlab (regexpressions might help, but I've never heard of them and
> > Matlab's help is not enough to get me up to speed).
>
> >> s='lorenzData_R0.9_z26.499398169954013.csv'
> s =
> lorenzData_R0.9_z26.499398169954013.csv
> >> v = sscanf(s,'lorenzData_R%f')
> v =
> 0.9000
> >>
>
> --

Thanks. I'm still getting used to these output strings.

Subject: How do I extract data from a string

From: dpb

Date: 7 Mar, 2010 14:46:02

Message: 5 of 7

Jeff wrote:
> dpb <none@non.net> wrote in message
> <hmvdaj$e7r$1@news.eternal-september.org>...
...

>> >> s='lorenzData_R0.9_z26.499398169954013.csv'
>> s =
>> lorenzData_R0.9_z26.499398169954013.csv
>> >> v = sscanf(s,'lorenzData_R%f')
>> v =
>> 0.9000
>> >>
...

> Thanks. I'm still getting used to these output strings.

Not sure which "output" you're speaking of; it's simply using the fixed
string in the input string as a literal string in the format string
which behaves a la C to match that specific string in the input and
ignore it in the subsequent input conversion specifier (%f for a float
here).

If you were also interested in the following value, expanding on a theme,

 >> v = sscanf(s,'lorenzData_R%f_z%f')
v =
     0.9000
    26.4994
 >>

--

Subject: How do I extract data from a string

From: Jeff

Date: 10 Mar, 2010 02:10:27

Message: 6 of 7

dpb <none@non.net> wrote in message <hn0e9v$32i$1@news.eternal-september.org>...
> Jeff wrote:
> > dpb <none@non.net> wrote in message
> > <hmvdaj$e7r$1@news.eternal-september.org>...
> ...
>
> >> >> s='lorenzData_R0.9_z26.499398169954013.csv'
> >> s =
> >> lorenzData_R0.9_z26.499398169954013.csv
> >> >> v = sscanf(s,'lorenzData_R%f')
> >> v =
> >> 0.9000
> >> >>
> ...
>
> > Thanks. I'm still getting used to these output strings.
>
> Not sure which "output" you're speaking of; it's simply using the fixed
> string in the input string as a literal string in the format string
> which behaves a la C to match that specific string in the input and
> ignore it in the subsequent input conversion specifier (%f for a float
> here).
>
> If you were also interested in the following value, expanding on a theme,
>
> >> v = sscanf(s,'lorenzData_R%f_z%f')
> v =
> 0.9000
> 26.4994
> >>
>
> --

Hi. I didn't see your reply till now because for some reason Mathworks is stopped emailing me when there are new responses.

The "a la C" behavior is what I'm still getting used to. I saw those types of outputs when I was in college, 20 years ago, and hadn't used them since. I've even been reluctant to ask how to put commas in my output, which I haven't been able to figure out, for fear of looking too stupid (actually, I didn't want to ask this question about strings either, for the same reason, but I needed it quickly for homework :D).

Thanks again.
-J

Subject: How do I extract data from a string

From: dpb

Date: 10 Mar, 2010 02:44:10

Message: 7 of 7

Jeff wrote:
...

> The "a la C" behavior is what I'm still getting used to. I saw those
> types of outputs when I was in college, 20 years ago, and hadn't used
> them since. I've even been reluctant to ask how to put commas in my
> output, which I haven't been able to figure out, for fear of looking too
> stupid (actually, I didn't want to ask this question about strings
> either, for the same reason, but I needed it quickly for homework :D).
...

Again, where does "outputs" come into play??? They're formatting
strings, not input or output. Yes, they are what's used to _translate_
internal<->external representations, but that's not the output itself.
Try to be both more precise in your terminology and your thinking will
probably come along quicker, too...

As for outputting _any_ text in conjunction w/ output values from
internal variables, just include the text in the formatting string.
Anything that isn't a recognized field is simply echoed. If you need a
special character such as a '%', double up to "escape" the character.

sprintf('This is an example: %d, %d', [1 2])

One thing you'll soon learn to appreciate is repmat() to replicate a
number of repeated fields in the formatting string as C (and hence,
Matlab) doesn't have the extremely useful field replication multiplier
of Fortran (something like 10F10.1, that is). To avoid having to write
10 %f fields in a row, you can use the repmat('%f10.1, 1, 10) instead.

--

Tags for this Thread

No tags are associated with 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