Info
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
Hello, I put a value to the variable >> i=1 i = 1 After that, I tried the following thing also. But it returns nothing. why does this happen so? >> k=char(i) k = >> Please help me. And please describe the reason for this problem and s
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hello, I put a value to the variable
>> i=1
i =
1
Where i is double type
After that, I tried the following thing also. But it returns nothing. why does this happen so?
>> k=char(i)
k =
>>
Please help me. And please describe the reason for this problem and suggest a remedy.
0 comentarios
Respuestas (2)
Mischa Kim
el 17 de Feb. de 2015
Editada: Mischa Kim
el 17 de Feb. de 2015
Joshy, you are trying to convert an integer to its corresponding ASCII character. If you check out an ASCII table you'll notice that "typical" characters start at integer value 32 (empty space), 33 (exclamation mark), etc. As an example, try
ii = 33;
k = char(ii)
k =
!
Also, I recommend not using i as a variable name, since it's also used by MATLAB as the imaginary unit.
John D'Errico
el 17 de Feb. de 2015
Editada: John D'Errico
el 17 de Feb. de 2015
What do you expect it to return? Read the help for char.
In fact, it does returns something. Just nothing that you are interested in. In fact, what it does generate is nothing MATLAB can even display, in any form that makes sense to you. So it dumps a white line to the screen.
Are you trying to convert the number 1 into a strong representation of the number 1 somehow? My wild guess is you are really interested in using the function sprintf, or possibly num2str.
Or maybe you are really just interested in creating the string '1' directly. It turns out that the ascii table has '1' as the 49th element. So had you done this instead:
c = char(49)
c =
1
you actually get the character string 1.
whos c
Name Size Bytes Class Attributes
c 1x1 2 char
MATLAB has indeed returned a string as I asked it to generate here.
Why? Because that is how ascii encoding table was set up, many years ago. Hey, you are the one who is somehow trying to work with ascii encodings.
In fact, we can see the ascii encodings for the digits 0 through 9 by doing this:
+'0123456789'
ans =
48 49 50 51 52 53 54 55 56 57
So I really have no idea what it is you were trying to do, but my guess is you were going about it wrong. I THINK you are trying to convert a number to astring.
0 comentarios
La pregunta está cerrada.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!