Info

此问题已关闭。 请重新打开它进行编辑或回答。

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 次查看(过去 30 天)
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.

回答(2 个)

Mischa Kim
Mischa Kim 2015-2-17
编辑:Mischa Kim 2015-2-17
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.
  1 个评论
Stephen23
Stephen23 2015-2-17
编辑:Stephen23 2015-2-17
Precisely. According to the ASCII standard, the character created with char(1) is called "Start of Header", and like all control characters it is non-printable.
+1 for mentioning the imaginary unit.

John D'Errico
John D'Errico 2015-2-17
编辑:John D'Errico 2015-2-17
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.

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by