Why are characters in MATLAB assigned numerical values?

8 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 16 Apr 2018
Edited: Stephen23 on 16 Apr 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":

Sign in to comment.

Answers (2)

Joost Meulenbeld
Joost Meulenbeld on 12 Mar 2018
Edited: Joost Meulenbeld on 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 Comment
Walter Roberson
Walter Roberson on 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.

Sign in to comment.


Walter Roberson
Walter Roberson on 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.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!