Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Topic: assign a number to a string
Replies: 2   Last Post: Dec 14, 2012 9:52 AM

 Messages: [ Previous | Next ]
assign a number to a string
Posted: Dec 14, 2012 6:40 AM

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

Date Subject Author