MATLAB Answers

convert an array of numbers into letters; 1 =a, 2 =b, 3 = c etc.

1,248 views (last 30 days)
David Mehr
David Mehr on 10 Apr 2013
Commented: Walter Roberson on 5 Jan 2016
So i have created a function that returns a row vector of numbers, Example:
[3, 6, 12, 1, 1, 3].
I want to write a new function that turns these numbers into letters. And in a form that i can call call it for any size set of results using a rule such as
1 = A, 2 = B, 3 = C, 4 = D, etc.
I see there is a num2str function but i dont really understand how to implement this as a function that will perform the conversion on each element in the vector.
What would be the best way to do this?

  0 Comments

Sign in to comment.

Answers (2)

Matt Kindig
Matt Kindig on 10 Apr 2013
Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
numbers = [3,6,12,1,1,3]
letters = Alphabet(numbers)
Or using the ASCII codes:
letters = char(numbers + 64);

  3 Comments

David Mehr
David Mehr on 10 Apr 2013
Thank you Matt for your swift response,
this is great (Y)
one quick question: is it possible to denote a character in your alphabet by more than one symbol?
E.G A a2 B b2 C c2 -
thank you :)
kclamb
kclamb on 5 Jan 2016
Alphabet = {'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'}
numbers = [3,6,12,1,1,3]
letters = Alphabet{1}(numbers)
letters = Alphabet{2}(numbers)
Walter Roberson
Walter Roberson on 5 Jan 2016
Substitutions = {'Alpha', 'Bob', 'Charlie', 'Delta'};
mapped = Substitutions(numbers);
strjoin(mapped, {' '}) %if you want them blank separated in one string

Sign in to comment.


Image Analyst
Image Analyst on 10 Apr 2013
Try this:
asciiValues = [3, 6, 12, 1, 1, 3]
asciiChars = char(asciiValues+'A'-1)
In the command window:
asciiValues =
3 6 12 1 1 3
asciiChars =
CFLAAC

  2 Comments

Image Analyst
Image Analyst on 10 Apr 2013
For lower case you can either do lower(asciiChars), or do this:
asciiChars = char(asciiValues+'a'-1)

Sign in to comment.