Why are characters in MATLAB assigned numerical values?

16 vues (au cours des 30 derniers jours)
Camille Levine
Camille Levine le 11 Mar 2018
Modifié(e) : Stephen23 le 16 Avr 2018
When I was trying to display a value next to a percent sign as a string, I accidentally used single quotes instead of double quotes, and it gave me this odd output. I realized that each non-numerical character is assigned a value in matlab, eg % = 37, ^ = 94, . = 46, etc. Even digits are assigned values, such as 0 = 48. Is there any reason or use for this feature or is it just a quirk of the language as a whole?
>> disp('%')
%
>> disp(0 + '%')
37
>> disp(0 + '^')
94
>> disp(0 + '.')
46
>> disp('^'+'0')
142
  2 commentaires
Roger Stafford
Roger Stafford le 12 Mar 2018
Modifié(e) : Walter Roberson le 12 Mar 2018
I recommend you read the Wikipedia article at:
Stephen23
Stephen23 le 16 Avr 2018
Modifié(e) : Stephen23 le 16 Avr 2018
"Is there any reason or use for this feature or is it just a quirk of the language as a whole?"
Absolutely everything on your computer is stored as numbers. Every photo, every film, every website, every program or app, ... they are all just made up of lots of numbers. Characters are no exception to this, although their encoding is, for historical reasons, certainly a bit "quirky":

Connectez-vous pour commenter.

Réponses (2)

Joost Meulenbeld
Joost Meulenbeld le 12 Mar 2018
Modifié(e) : Joost Meulenbeld le 12 Mar 2018
Using the plus operator assumes the operands (in this case a character and a numeral or two characters) to be numerics, and a character will then be converted to its ascii value. Look at i.e. https://www.asciitable.com/ for a table containing all character values.
  1 commentaire
Walter Roberson
Walter Roberson le 12 Mar 2018
As a technical quibble: MATLAB does not use ASCII, MATLAB uses the first 65536 entries of Unicode. ASCII is only defined up to location 127.

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 12 Mar 2018
Digital computers can only manipulate bits. A storage location is just a group of bits. There are not different kinds of external memory to store integers or floating point numbers or characters: there are just groups of bits together with programs to manipulate the bits. Everything else is a matter of how the programs tell the computer to interpret the bits. As far as a computer cares, it is entirely valid to take the group of bits that a moment ago was interpreted as a double precision number, and to say "Now that group of bits is to be interpreted as four signed 16 bit integers instead" and do calculations on the group of bits that way.
So too characters are just stored as groups of bits. Each different character has a different binary pattern. You can do arithmetic on the patterns and nothing cares. At some point, though, the user needs to be shown the character: at that point the binary pattern is processed through some code to look up information about how the character should be displayed on the screen. It is at that point that information about font and italics and color and point size are to be considered: those are display attributes rather than information about the character itself. The same binary pattern is used to represent an 'A' that is to be displayed in Monaco bold 17 point, as might be used to represent an 'A' that is to be displayed in Century New Schoolbook italic 12 point.
MATLAB represents characters as 16 bit unsigned numbers: up to 65536 different characters can be represented this way, each represented by a distinct binary pattern. There are some indications that internally MATLAB uses UTF16 encoding to represent additional characters, but that can be difficult to prove or disprove.
The binary pattern used for '%' is the same binary pattern that is used to represent uint16(37). If you do
fooc = '%';
food = uint16(37);
then the data pointers of the two variables will end up pointing to the same binary patterns: the difference will just be in the headers for the variable that tell MATLAB how the user wants the binary patterns to be interpreted.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by