Convert letters to numbers in cell

3 ビュー (過去 30 日間)
Milou
Milou 2012 年 7 月 16 日
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 件のコメント
Jan
Jan 2012 年 7 月 16 日
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.

サインインしてコメントする。

採用された回答

Sean de Wolski
Sean de Wolski 2012 年 7 月 16 日
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')
  1 件のコメント
Milou
Milou 2012 年 7 月 25 日
Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by