Convert letters to numbers in cell

2 views (last 30 days)
Hello,
I have a cell array that looks like :
{A, E, B, A, D, C; B, A, A, D, C, E}
for 9x28 cell
I want to replace A's with 5 for columns 1, 2, 4, 6, 8, 11, 14, 17, 19, 22, 23 ,24 ,26 27 and replace E's with 5 for columns 3, 5, 7, 9, 10, 12, 13, 15, 16, 18, 20, 21, 25, 28
so that for some columns, A-E ranges from 5-1 and for others A-E ranges from 1-5.
i have tried:
mydata = cell2mat(mydata);
mydata(mydata == 'A') = '1';
mydata(mydata == 'B') = '2';
mydata(mydata == 'C') = '3';
mydata(mydata == 'D') = '4';
mydata(mydata == 'E') = '5';
but this of course is not column specific. is there anyway i can isolate columns?
also, when i try to convert it back to a double with:
mydata= str2double(mydata) I get all NaN values
this does not work either:
y = sprintf('%s', mydata{:}):
x = sscanf(y,'%f');
i get the error: Expression or statement is incomplete or incorrect.
what can i do?
Thank you so much for your time and help!
  1 Comment
Jan
Jan on 16 Jul 2012
I do not understand the question. It is not clear, when you are talking about numbers and when about characters. I do not know mydata and cannot run the code you have posted. Please clarify the question by editing it.

Sign in to comment.

Accepted Answer

Sean de Wolski
Sean de Wolski on 16 Jul 2012
Assuming these are all strings (which I am not sure from your description) I would just use regexprep with proper indexing:
C = {'A', 'E', 'B', 'A', 'D', 'C'; 'B', 'A', 'A', 'D', 'C', 'E'}
C(:,1:2:end) = regexprep(C(:,1:2:end),'A','5')
C(:,2:2:end) = regexprep(C(:,2:2:end),'E','5')

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!