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 up a string into smaller strings

Subject: Splitting up a string into smaller strings

From: Philip

Date: 29 Jul, 2009 19:42:01

Message: 1 of 4

I have a string:

>> line = # GHZ S RI R 50.000000;

I want to grab each element as an individual string, and then convert the last into a number. After looking around, I tried this and got an error:

>> header = regexp(huh,' ','split')
header =
   '#' 'GHZ' 'S' 'RI' 'R' '50.000000'
>> str2num(header(6))
??? Error using ==> str2num at 33
Requires string or character array input.

For all I can tell, header looks like a matrix of strings so I don't know why str2num isn't accepting it. Maybe it's just because I don't understand all this regular expression nonsense, but what am I doing wrong? Is there a better way to split this string? Thanks

Subject: Splitting up a string into smaller strings

From: Nathan

Date: 29 Jul, 2009 19:53:13

Message: 2 of 4

On Jul 29, 12:42 pm, "Philip " <peroi...@gmail.com> wrote:
> I have a string:
>
> >> line = # GHZ S RI R 50.000000;
>
> I want to grab each element as an individual string, and then convert the last into a number. After looking around, I tried this and got an error:
>
> >> header = regexp(huh,' ','split')
>
> header =
>    '#'    'GHZ'    'S'    'RI'    'R'    '50.000000'>> str2num(header(6))
>
> ??? Error using ==> str2num at 33
> Requires string or character array input.
>
> For all I can tell, header looks like a matrix of strings so I don't know why str2num isn't accepting it. Maybe it's just because I don't understand all this regular expression nonsense, but what am I doing wrong? Is there a better way to split this string? Thanks

How about
str2num(header{6})

Except this will get rid of those decimal places...

BTW: Braces pull out the value at that location in a cell.

-Nathan

Subject: Splitting up a string into smaller strings

From: Dave Brackett

Date: 29 Jul, 2009 19:55:20

Message: 3 of 4

"Philip " <peroikos@gmail.com> wrote in message <h4q8m8$5i8$1@fred.mathworks.com>...
> I have a string:
>
> >> line = # GHZ S RI R 50.000000;
>
> I want to grab each element as an individual string, and then convert the last into a number. After looking around, I tried this and got an error:
>
> >> header = regexp(huh,' ','split')
> header =
> '#' 'GHZ' 'S' 'RI' 'R' '50.000000'
> >> str2num(header(6))
> ??? Error using ==> str2num at 33
> Requires string or character array input.
>
> For all I can tell, header looks like a matrix of strings so I don't know why str2num isn't accepting it. Maybe it's just because I don't understand all this regular expression nonsense, but what am I doing wrong? Is there a better way to split this string? Thanks


try str2num(header{6}) as header is a cell.

Subject: Splitting up a string into smaller strings

From: Philip

Date: 29 Jul, 2009 20:04:01

Message: 4 of 4

Oh ok, I didn't know about cells, and how you use them. Thanks Nathan and David

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