필터 지우기
필터 지우기

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일)
JOSHY T R
JOSHY T R 2015년 2월 17일
마감: MATLAB Answer Bot 2021년 8월 20일
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