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:
Parsing Strings

Subject: Parsing Strings

From: Kevin Ellis

Date: 13 Nov, 2012 21:56:20

Message: 1 of 4

Hello,

I have a question regarding parsing strings. Suppose I have a string:

S = '42.714 GAL OF SMALL BOAT FUEL @3.699 PER GAL.'

I have been trying to figure out a way to remove the numbers 42.714 and 3.699 from the string. I have tried using 'sscanf' and 'regexp' to do this. Using regexp I can find the index of the numbers:

idx = regexp(S,'\d')

idx =

     1 2 4 5 6 32 34 35 36

But this is not working because the decimal point is not being identified. For my problem, it is not safe to assume that the character in index 3 is always a decimal point. It may just be a blank space. Thus, I was wondering if there was a better way to approach this problem. Again, I am simply looking to remove the numbers from the string (decimal point included). Any help would be much appreciated.

Kevin

Subject: Parsing Strings

From: dpb

Date: 13 Nov, 2012 23:48:28

Message: 2 of 4

On 11/13/2012 3:56 PM, Kevin Ellis wrote:
> Hello,
>
> I have a question regarding parsing strings. Suppose I have a string:
>
> S = '42.714 GAL OF SMALL BOAT FUEL @3.699 PER GAL.'
>
> I have been trying to figure out a way to remove the numbers 42.714 and
> 3.699 from the string. I have tried using 'sscanf' and 'regexp' to do
> this. Using regexp I can find the index of the numbers:
...

Is the text fixed or variable?

If it's fixed and simply values change you can use

x = sscanf(S,'%f GAL OF SMALL BOAT FUEL @%f');

sscanf() isn't flexible in mixing conversion of string and numeric
values but textscan() can.

In regexp the expression needed would be (assuming F format,not E)

s=regexp(S,'[0-9.]','match');

then convert the resulting strings.

--

Subject: Parsing Strings

From: Kevin Ellis

Date: 13 Nov, 2012 23:58:16

Message: 3 of 4

dpb <none@non.net> wrote in message <k7uma4$f1d$1@speranza.aioe.org>...
> On 11/13/2012 3:56 PM, Kevin Ellis wrote:
> > Hello,
> >
> > I have a question regarding parsing strings. Suppose I have a string:
> >
> > S = '42.714 GAL OF SMALL BOAT FUEL @3.699 PER GAL.'
> >
> > I have been trying to figure out a way to remove the numbers 42.714 and
> > 3.699 from the string. I have tried using 'sscanf' and 'regexp' to do
> > this. Using regexp I can find the index of the numbers:
> ...
>
> Is the text fixed or variable?
>
> If it's fixed and simply values change you can use
>
> x = sscanf(S,'%f GAL OF SMALL BOAT FUEL @%f');
>
> sscanf() isn't flexible in mixing conversion of string and numeric
> values but textscan() can.
>
> In regexp the expression needed would be (assuming F format,not E)
>
> s=regexp(S,'[0-9.]','match');
>
> then convert the resulting strings.
>
> --

The text is variable. The placement of the numbers will change and also the price per gallon may not appear at all. How can textscan() help me?

Kevin

Subject: Parsing Strings

From: Kevin Ellis

Date: 14 Nov, 2012 00:19:07

Message: 4 of 4

"Kevin Ellis" wrote in message <k7umqo$mph$1@newscl01ah.mathworks.com>...
> dpb <none@non.net> wrote in message <k7uma4$f1d$1@speranza.aioe.org>...
> > On 11/13/2012 3:56 PM, Kevin Ellis wrote:
> > > Hello,
> > >
> > > I have a question regarding parsing strings. Suppose I have a string:
> > >
> > > S = '42.714 GAL OF SMALL BOAT FUEL @3.699 PER GAL.'
> > >
> > > I have been trying to figure out a way to remove the numbers 42.714 and
> > > 3.699 from the string. I have tried using 'sscanf' and 'regexp' to do
> > > this. Using regexp I can find the index of the numbers:
> > ...
> >
> > Is the text fixed or variable?
> >
> > If it's fixed and simply values change you can use
> >
> > x = sscanf(S,'%f GAL OF SMALL BOAT FUEL @%f');
> >
> > sscanf() isn't flexible in mixing conversion of string and numeric
> > values but textscan() can.
> >
> > In regexp the expression needed would be (assuming F format,not E)
> >
> > s=regexp(S,'[0-9.]','match');
> >
> > then convert the resulting strings.
> >
> > --
>
> The text is variable. The placement of the numbers will change and also the price per gallon may not appear at all. How can textscan() help me?
>
> Kevin

I looked into the help file for textscan(). That could actually work. I tried to read the string into a cell array using textscan().

C = textscan(S,'%s')

C =
        {9x1} cell

Is there any way to read each individual word separated by a blank space into a cell array of size {1,9}? More explicitly, is there a way to read each word of the string into an individual cell element? That would be more helpful to me.

Kevin

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