Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

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 vue (au cours des 30 derniers jours)
JOSHY T R
JOSHY T R le 17 Fév 2015
Clôturé : MATLAB Answer Bot le 20 Août 2021
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.

Réponses (2)

Mischa Kim
Mischa Kim le 17 Fév 2015
Modifié(e) : Mischa Kim le 17 Fév 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.
  1 commentaire
Stephen23
Stephen23 le 17 Fév 2015
Modifié(e) : Stephen23 le 17 Fév 2015
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 le 17 Fév 2015
Modifié(e) : John D'Errico le 17 Fév 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.

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by