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:
help on character numbers

Subject: help on character numbers

From: hd

Date: 1 Dec, 2008 20:43:12

Message: 1 of 9

Hi,

I have this list of strings

AB11fever
AC22bronchitis
ADE153Chickenpox


and I want to extract the numbers separately and the characters
separately ie
the output should be 2 vectors

ABfever 11
ACbronchitis 22
ADEChickenpox 153

I was wondering what would be a good way to do this.
I was thinking if I convert each of thea bove to characters and then
try to identify if each character is a number. But I am not sure if
there is something in MATLAB that does this.
Any help is appreciated

many thanks

Subject: help on character numbers

From: Rune Allnor

Date: 1 Dec, 2008 20:57:25

Message: 2 of 9

On 1 Des, 21:43, hd <heen...@gmail.com> wrote:
> Hi,
>
> I have this list of strings
>
> AB11fever
> AC22bronchitis
> ADE153Chickenpox
>
> and I want to extract the =A0numbers separately and the characters
> separately ie
> the output should be 2 vectors
>
> ABfever =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 11
> ACbronchitis =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A022
> ADEChickenpox =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 153
>
> I was wondering what would be a good way to do this.
> I was thinking if I convert each of thea bove to characters and then
> try to identify if each character is a number. But I am not sure if
> there is something in MATLAB that does this.

Regular expressions?

Rune

Subject: help on character numbers

From: Yuri Geshelin

Date: 1 Dec, 2008 20:59:02

Message: 3 of 9

hd <heenagd@gmail.com> wrote in message <a15746b2-abd0-434d-b2b8-2d1d406a3218@j11g2000yqg.googlegroups.com>...
> Hi,
>
> I have this list of strings
>
> AB11fever
> AC22bronchitis
> ADE153Chickenpox
>
>
> and I want to extract the numbers separately and the characters
> separately ie
> the output should be 2 vectors
>
> ABfever 11
> ACbronchitis 22
> ADEChickenpox 153
>
> I was wondering what would be a good way to do this.
> I was thinking if I convert each of thea bove to characters and then
> try to identify if each character is a number. But I am not sure if
> there is something in MATLAB that does this.
> Any help is appreciated
>
> many thanks

Hi,

s={'AB11fever','AC22bronchitis','ADE153Chickenpox'};
for i = 1 : length(s)
    digit_index = find(s{i}>=48&s{i}<=57);
    sdigit = s{i}(digit_index);
    s{i}(digit_index)=[];
    disp([s{i} ' ' sdigit])
end

Yuri

Subject: help on character numbers

From: Bruno Luong

Date: 1 Dec, 2008 21:10:19

Message: 4 of 9

c={'AB11fever'
    'AC22bronchitis'
    'ADE153Chickenpox'}

splitfun=@(s) deal(s(s>='0' & s<='9'),s(s<'0' | s>'9'));

[num str]=cellfun(splitfun,c,'UniformOutput',false)

% Bruno

Subject: help on character numbers

From: Matt Fig

Date: 2 Dec, 2008 03:30:18

Message: 5 of 9

Just to follow up on Rune's idea:


c={'AB11fever'
    'AC22bronchitis'
    'ADE153Chickenpox'};

str = regexprep(c,'\d*','');
num = regexprep(c,'[^\d]*','');

Subject: help on character numbers

From: Yuri Geshelin

Date: 2 Dec, 2008 13:58:02

Message: 6 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gh1jrr$s42$1@fred.mathworks.com>...
> c={'AB11fever'
> 'AC22bronchitis'
> 'ADE153Chickenpox'}
>
> splitfun=@(s) deal(s(s>='0' & s<='9'),s(s<'0' | s>'9'));
>
> [num str]=cellfun(splitfun,c,'UniformOutput',false)
>
> % Bruno
>

Hi,

I tried this in MATLAB Version 7.0.4.365 (R14) Service Pack 2 (Windows XP).
Here is what happens
>> splitfun=@(s) deal(s(s>='0' & s<='9'),s(s<'0' | s>'9'));
>> [num str]=cellfun(splitfun,c,'UniformOutput',false)
??? Too many inputs.

Yuri

Subject: help on character numbers

From: Matt Fig

Date: 2 Dec, 2008 14:40:17

Message: 7 of 9

"Yuri Geshelin" <geshelin@hotmail.com> wrote in message
> I tried this in MATLAB Version 7.0.4.365 (R14) Service Pack 2 (Windows XP).
> Here is what happens
> >> splitfun=@(s) deal(s(s>='0' & s<='9'),s(s<'0' | s>'9'));
> >> [num str]=cellfun(splitfun,c,'UniformOutput',false)
> ??? Too many inputs.
>
> Yuri

Is the error on cellfun? If I remember correctly, the 'UniformOutput' wasn't added till after 7.0.

Subject: help on character numbers

From: Bruno Luong

Date: 2 Dec, 2008 17:23:01

Message: 8 of 9

"Matt Fig" <spamanon@yahoo.com> wrote in message <gh3hch$4t$1@fred.mathworks.com>...

>
> Is the error on cellfun? If I remember correctly, the 'UniformOutput' wasn't added till after 7.0.

Well then may be a safer way is to get back to an old for-loop:

c={'AB11fever'
    'AC22bronchitis'
    'ADE153Chickenpox'};

splitfun=@(s) deal(s(s>='0' & s<='9'),s(s<'0' | s>'9'));

num=cell(size(c)); str=cell(size(c));
for k=1:length(c)
    [num{k} str{k}] = splitfun(c{k});
end

num
str

% Bruno

Subject: help on character numbers

From: Matt Fig

Date: 2 Dec, 2008 17:52:01

Message: 9 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
> Well then may be a safer way is to get back to an old for-loop:

...which is even faster than the regexprep or cellfun solutions!

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