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:
assign a number to a string

Subject: assign a number to a string

From: Saad

Date: 14 Dec, 2012 11:40:08

Message: 1 of 3

Dear all,

I have a list of string:

txt={'BBB3', 'BBB2', 'BBB1', 'A3', 'A2', 'A1','AA3','AA2','AA1'};

i would like to assign a value to each string. For instance,
 

Here is my code
for i=1:length(txt)
           if isequal(txt{'i'},'BBB3')

             txt{'i'}=11;
   
           elseif isequal(txt{'i'},'BBB2')

            txt{'i'}=12;

            elseif isequal(txt{'i'},'BBB1')
            txt{'i'}=13;

             elseif isequal(txt{'i'},'A3')

                 txt{'i'}=14;
                 
           elseif isequal(txt{'i'},'A2')
               txt{'i'}=15;
           elseif isequal(txt{'i'},'A1')
                    txt{'i'}=16;
             elseif isequal(txt{'i'},'AA3')
                 txt{'i'}=17;
             elseif isequal(txt{'i'},'AA2')
                 txt{'i'}=18;
            else
               txt{'i'}=20;
           end
            
         end

Basically the output I am looking for is:

txt={11, 12, 13, 14, 15, 16, 17, 18 ,19};

however when I run the code and type txt in the screen in order to retrieve the final output i still get the initial input which is txt={'BBB3', 'BBB2', 'BBB1', 'A3', 'A2', 'A1','AA3','AA2','AA1'};

I might have missed something somewhere. ANy help is much appreciated Thanks

Subject: assign a number to a string

From: bartekltg

Date: 14 Dec, 2012 11:56:06

Message: 2 of 3

W dniu 2012-12-14 12:40, Saad pisze:
> Dear all,
>
> I have a list of string:
>
> txt={'BBB3', 'BBB2', 'BBB1', 'A3', 'A2', 'A1','AA3','AA2','AA1'};
>
> i would like to assign a value to each string. For instance,
>
>
> Here is my code
> for i=1:length(txt)
> if isequal(txt{'i'},'BBB3')


Why you use quotation marks?

'i' is character i, and it is equal 105.
105-th cell of txt is not equal 'BBB3' :)

  if isequal(txt{i},'BBB3')

BTW, have you heard about switch-case expression?


for i=1:length(txt)
switch( txt{i} )
case 'BBB2'
                    txt{i} = 11
                 case 'BBB1'
                    txt {i} = 12
                 .....
  end


bartekltg

Subject: assign a number to a string

From: Steven_Lord

Date: 14 Dec, 2012 14:52:45

Message: 3 of 3



"Saad " <saad.badaoui@edhec.edu> wrote in message
news:kaf36o$9ja$1@newscl01ah.mathworks.com...
> Dear all,
>
> I have a list of string:
>
> txt={'BBB3', 'BBB2', 'BBB1', 'A3', 'A2', 'A1','AA3','AA2','AA1'};
>
> i would like to assign a value to each string. For instance,

So you want to replace each string with a numeric value? That's easy.

> Here is my code
> for i=1:length(txt)
> if isequal(txt{'i'},'BBB3')

This attempts to check element double('i') [= 105] of txt to see if it is
equal to 'BBB3'. If you want to check if element i of txt is equal to
'BBB3', you need to say:

if isequal(txt{i}, 'BBB3')

> txt{'i'}=11;

I would recommend against making these changes in-place. Create a separate
numeric vector:

numericCodes = zeros(size(txt));

and assign into the elements of that vector:

numericCodes(i) = 11;

*snip*

One way you can simplify this is to use STRCMP.


% Define the mapping data
stringsToMap = {'BBB3', 'BBB2', 'BBB1', 'A3', 'A2', 'A1','AA3','AA2','AA1'};
valuesToMap = [11, 12, 13, 14, 15, 16, 17, 18, 19];

% Define the data to be processed and the result
txt={'BBB3', 'BBB2', 'BBB1', 'A3', 'A2', 'junk', 'A1','AA3','AA2','AA1'};
numericCodes = repmat(20, size(txt)); % Since 20 is 'not present' we'll use
that to preallocate

% Do the work
for whichMap = 1:numel(stringsToMap)
    % Find the matches
    locateValues = strcmp(txt, stringsToMap{whichMap});

    % Fill in the appropriate elements of numericCodes using the logical
mask
    numericCodes(locateValues) = valuesToMap(whichMap);
end


Your original code runs one loop iteration per element in txt and if that
cell array is large, that could take a good deal of time. This version
iterates over the strings for which you're checking, which I'm guessing is
smaller (perhaps MUCH smaller) than txt. It also avoids having to modify the
actual mapping code if you add a new string that needs to be mapped to a
value; instead you need to modify the mapping _data_. Note for example that
the code you posted doesn't have a case for 'AA1'; looking at the code I
posted it's trivial to check (by looking at stringsToMap and the
corresponding element of valuesToMap) that it will perform the mapping for
'AA1'.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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