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:
splitting strings

Subject: splitting strings

From: Jane T

Date: 24 Oct, 2011 22:55:31

Message: 1 of 5

I have a string that I want to split by two different delimiters; commas and asterix'.

for the string:

idn = '$GFDTA, 0.0,0,22.8,6214,2011/10/24 16:41:44, NH3OP-1035,1*63'

I can use the code

delim = regexp(idn,',*','split');

and it works great, however it doesn't work for the string

idn = '$GFDBG, 41.4, 154, 9719, 98, 1, 1197, 0, 1, 0*4A'

in this case '0*4A' hasn't been split at the *.

Can anyone please help me understand why this is (I think it may be due to the hexadecimal characters, but don't understand why exactly) and help me fix the code?

Many thanks
Jane

Subject: splitting strings

From: TideMan

Date: 25 Oct, 2011 01:34:01

Message: 2 of 5

On Oct 25, 11:55 am, "Jane T" <jte...@boreal-laser.com> wrote:
> I have a string that I want to split by two different delimiters; commas and asterix'.
>
> for the string:
>
> idn = '$GFDTA,     0.0,0,22.8,6214,2011/10/24 16:41:44, NH3OP-1035,1*63'
>
> I can use the code
>
> delim = regexp(idn,',*','split');
>
> and it works great, however it doesn't work for the string
>
> idn = '$GFDBG,  41.4, 154, 9719, 98, 1, 1197, 0, 1, 0*4A'
>
> in this case '0*4A' hasn't been split at the *.
>
> Can anyone please help me understand why this is (I think it may be due to the hexadecimal characters, but don't understand why exactly) and help me fix the code?
>
> Many thanks
> Jane

I'm no expert with regular expressions and my version of Matlab does
not allow 'split', but I think this is not working as you think it is
because * has a specific meaning, so you need to quote it using \.
Try this:
delim = regexp(idn,{'\*',','});

Subject: splitting strings

From: Jane T

Date: 25 Oct, 2011 02:12:28

Message: 3 of 5

TideMan <mulgor@gmail.com> wrote in message <ea77176f-451b-465b-8977-cf148a234587@hj4g2000vbb.googlegroups.com>...
> On Oct 25, 11:55 am, "Jane T" <jte...@boreal-laser.com> wrote:
> > I have a string that I want to split by two different delimiters; commas and asterix'.
> >
> > for the string:
> >
> > idn = '$GFDTA,     0.0,0,22.8,6214,2011/10/24 16:41:44, NH3OP-1035,1*63'
> >
> > I can use the code
> >
> > delim = regexp(idn,',*','split');
> >
> > and it works great, however it doesn't work for the string
> >
> > idn = '$GFDBG,  41.4, 154, 9719, 98, 1, 1197, 0, 1, 0*4A'
> >
> > in this case '0*4A' hasn't been split at the *.
> >
> > Can anyone please help me understand why this is (I think it may be due to the hexadecimal characters, but don't understand why exactly) and help me fix the code?
> >
> > Many thanks
> > Jane
>
> I'm no expert with regular expressions and my version of Matlab does
> not allow 'split', but I think this is not working as you think it is
> because * has a specific meaning, so you need to quote it using \.
> Try this:
> delim = regexp(idn,{'\*',','});

Thanks for your suggestion, unfortunately that doesn't seem to be the solution. The result is a cell array where the first cell contains the result of the string being comma delimited and the second element the result of the string being delimited by an asterix. What I don't understand is why is does exactly what I want in one instance, but not the other.

Subject: splitting strings

From: Doug Schwarz

Date: 25 Oct, 2011 05:10:51

Message: 4 of 5

In article <j84qd2$qgr$1@newscl01ah.mathworks.com>,
 "Jane T" <jterry@boreal-laser.com> wrote:

> I have a string that I want to split by two different delimiters; commas and
> asterix'.
>
> for the string:
>
> idn = '$GFDTA, 0.0,0,22.8,6214,2011/10/24 16:41:44, NH3OP-1035,1*63'
>
> I can use the code
>
> delim = regexp(idn,',*','split');
>
> and it works great, however it doesn't work for the string
>
> idn = '$GFDBG, 41.4, 154, 9719, 98, 1, 1197, 0, 1, 0*4A'
>
> in this case '0*4A' hasn't been split at the *.
>
> Can anyone please help me understand why this is (I think it may be due to
> the hexadecimal characters, but don't understand why exactly) and help me fix
> the code?
>
> Many thanks
> Jane

Neither version of idn is split properly using ',*' because that means
to split at 'zero or more commas'. If the first version of idn is split
properly using ',*' I don't understand why -- and it doesn't work that
way in my version of MATLAB, R2011a.

Use ',|\*' which means split at 'a comma OR at an asterisk'. Works for
me.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: splitting strings

From: Jane T

Date: 25 Oct, 2011 13:19:28

Message: 5 of 5

Perfect, thank you Doug

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