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:
convert text to number

Subject: convert text to number

From: Jonathan

Date: 9 Mar, 2010 07:24:06

Message: 1 of 7

I have a column of string values characterized as either 'wet' or 'dry'. What I would like to do is convert the 'wet' strings to a 1 and the 'dry' to zero.

My code is :

n=length(Wet_Dry);
wetdry=zeros(n,1);   %initialize wetdry field

for i=1:n
    if Wet_Dry(i)=='Wet'
        wetdry(i)=1;
    else wetdry(i)=2;
    end
end 

I'm getting an error with the equal sign and it is not clear what I am doing wrong. Any help would be much appreciated.

Thanks
Jon

Subject: convert text to number

From: TideMan

Date: 9 Mar, 2010 07:43:44

Message: 2 of 7

On Mar 9, 8:24 pm, "Jonathan" <jkak...@yahoo.co.uk> wrote:
> I have a column of string values characterized as either 'wet' or 'dry'.  What I would like to do is convert the 'wet' strings to a 1 and the 'dry' to zero.
>
> My code is :
>
> n=length(Wet_Dry);
> wetdry=zeros(n,1);   %initialize wetdry field
>
> for i=1:n
>     if Wet_Dry(i)=='Wet'
>         wetdry(i)=1;
>     else wetdry(i)=2;
>     end
> end 
>
> I'm getting an error with the equal sign and it is not clear what I am doing wrong.  Any help would be much appreciated.
>
> Thanks
> Jon

help strcmp

Subject: convert text to number

From: Jos (10584)

Date: 9 Mar, 2010 09:59:05

Message: 3 of 7

"Jonathan" <jkakiwi@yahoo.co.uk> wrote in message <hn4t2m$kap$1@fred.mathworks.com>...
> I have a column of string values characterized as either 'wet' or 'dry'. What I would like to do is convert the 'wet' strings to a 1 and the 'dry' to zero.
>
> My code is :
>
> n=length(Wet_Dry);
> wetdry=zeros(n,1);   %initialize wetdry field
>
> for i=1:n
>     if Wet_Dry(i)=='Wet'
>         wetdry(i)=1;
>     else wetdry(i)=2;
>     end
> end 
>
> I'm getting an error with the equal sign and it is not clear what I am doing wrong. Any help would be much appreciated.
>
> Thanks
> Jon

Ask yourself a couple of questions:

1) Of what type is the variable "Wet_Dry"
Is it a character array or a cell array of strings

2) What does Wet_Dry(1) give?
A single character or a cell

3) Try these
  'W' == 'Wet' % Do you get the same error?
  WetDry(1,:) == 'Wet'

Jos

Subject: convert text to number

From: Jonathan

Date: 9 Mar, 2010 14:28:05

Message: 4 of 7

I have a matrix that is for example 1000 by 13. While the bulk of the columns are numbers, one of the columns has text in it that is either 'Wet' or 'Dry'. What I would like to do is simply change the 'Wet' text to 1 and the 'Dry' text to 0. I don't have matlab in front of me and will try these various options soon. I did have a look at strcmp but did not see anything obvious.

Thanks Jon

"Jos (10584) " <#10584@fileexchange.com> wrote in message <hn5659$8i7$1@fred.mathworks.com>...
> "Jonathan" <jkakiwi@yahoo.co.uk> wrote in message <hn4t2m$kap$1@fred.mathworks.com>...
> > I have a column of string values characterized as either 'wet' or 'dry'. What I would like to do is convert the 'wet' strings to a 1 and the 'dry' to zero.
> >
> > My code is :
> >
> > n=length(Wet_Dry);
> > wetdry=zeros(n,1);   %initialize wetdry field
> >
> > for i=1:n
> >     if Wet_Dry(i)=='Wet'
> >         wetdry(i)=1;
> >     else wetdry(i)=2;
> >     end
> > end 
> >
> > I'm getting an error with the equal sign and it is not clear what I am doing wrong. Any help would be much appreciated.
> >
> > Thanks
> > Jon
>
> Ask yourself a couple of questions:
>
> 1) Of what type is the variable "Wet_Dry"
> Is it a character array or a cell array of strings
>
> 2) What does Wet_Dry(1) give?
> A single character or a cell
>
> 3) Try these
> 'W' == 'Wet' % Do you get the same error?
> WetDry(1,:) == 'Wet'
>
> Jos

Subject: convert text to number

From: Jan Simon

Date: 9 Mar, 2010 14:42:04

Message: 5 of 7

Dear Jonathan!

> I have a column of string values characterized as either 'wet' or 'dry'. What I would like to do is convert the 'wet' strings to a 1 and the 'dry' to zero.
>
> My code is :
>
> n=length(Wet_Dry);
> wetdry=zeros(n,1);   %initialize wetdry field
>
> for i=1:n
>     if Wet_Dry(i)=='Wet'
>         wetdry(i)=1;
>     else wetdry(i)=2;
>     end
> end 
>
> I'm getting an error with the equal sign and it is not clear what I am doing wrong. Any help would be much appreciated.

Please post the exact error message the corresponding line of code ever.
Shall we guess your problem ??

Nevertheless, this looks suspicious:
  if Wet_Dry(i)=='Wet'
If Wet_Dry is a cell, you try to compare a cell with a [1 x 3] char array. This causes an error obviously. This is more likely what you want:
  if strcmp(Wet_Dry, 'Wet')

A faster solution would be to omit the loop:
  wetdry = ones(n, 1);
  wetdry(strcmp(Wet_Dry, 'Dry')) = 2;

Good luck, Jan

Subject: convert text to number

From: Husam Aldahiyat

Date: 9 Mar, 2010 14:43:02

Message: 6 of 7

You mean its a cell array right?
If the array is a cell array then you might want to take a look at cellfun().

Subject: convert text to number

From: Walter Roberson

Date: 9 Mar, 2010 15:20:28

Message: 7 of 7

Jonathan wrote:
> I have a matrix that is for example 1000 by 13. While the bulk of the
> columns are numbers, one of the columns has text in it that is either
> 'Wet' or 'Dry'.

You cannot mix text and numbers in the same matrix unless you use a cell
array.


> What I would like to do is simply change the 'Wet' text
> to 1 and the 'Dry' text to 0.

Multiple ways:

cellfun(@(v) v(1) == 'W', Wet_Dry)


ismember(Wet_Dry, {'Wet'})


T=char(Wet_Dry); T(:,1) == 'W'

char(Wet_Dry) * [1;0;0] == 'W'


There's probably also an approach using bsxfun.

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