why ascii instead of letter?

3 visualizaciones (últimos 30 días)
huda nawaf
huda nawaf el 19 de Sept. de 2011
hi,
I want place 'a' instead of 1 as in :
x=[1 2 3 1]; >> for i=1:4 if x(i)==1 x(i)='a' end end
but the result: 97 2 3 97
why ascii is appearing?

Respuestas (2)

Walter Roberson
Walter Roberson el 19 de Sept. de 2011
You have initialized a numeric array rather than a character array. Numeric arrays cannot hold characters as characters. Only cell arrays can mix numerics with characters.

Fangjun Jiang
Fangjun Jiang el 19 de Sept. de 2011
Original x is a double array, but in your for-loop, you assign a char value to its element. So it takes the ASCII value of letter 'a'. see
double('a')
  2 comentarios
huda nawaf
huda nawaf el 20 de Sept. de 2011
so , what I have to do?
thanks
Fangjun Jiang
Fangjun Jiang el 20 de Sept. de 2011
use cell array
%%
x={1 2 3 1};
for i=1:4
if x{i}==1
x{i}='a';
end
end

Iniciar sesión para comentar.

Categorías

Más información sobre Operators and Elementary Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by