How can you find the ascii value of a character?
This question arose after noticing that num2str(x) returns the ascii
character corresponding to the number x. The question is how to go
the other way. We know, for example, that ['0' 66] results in the
string 0B, thereby mapping the number 66 to the character
B but a more straightforward solution is to use char(66)
which returns B. For fun you can try the following:
>> x=32:1:126;
>> char(x)
The result is
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
which shows all the printable ascii characters in order.
What is important about this question is the procedure used to find
the answer. What I did was the following:
- Realized that what we were looking for is something that appears to be
the inverse of num2str.
- Guessed the inverse might be str2num.
- Did help str2num.
- This was not exactly what is called for because it only works on
ascii characters that represent numbers.
- At the bottom of the help page there is a see also section.
- The function char was listed there.
- Did help char.
- Determined that is what is wanted.
The above procedure illustrates how you will learn more about MATLAB as you
use the tool. You will be coding and determine a need for a function. Then
you will need to check MATLAB for the existence of that function. Doing so
efficiently is, obviously, important.