MATLAB: Does matlab allow array indexing by a string by converting them into ascii codes

indexingMATLABmatrix manipulation

If you (by accident) enter a character array in an array indexing and it returns values not an error. It turns out that MATLAB converts characters to ASCII codes, use them as integer indices, and returns the values. For example:
X = rand(1,100);
X('A') == X(65)
I think this is just confusing, useless, and potentially risky. Does anyone know a good reason why MATLAB even supports this?

Best Answer

In many languages, and certainly in the majority of languages when matlab was created, the character type is just an alias for an integer type, and thus there was absolutely no reason for characters to be treated any different in matlab.
Modern languages do tend to treat the string (not char!) type differently and indeed matlab does too now. As dpb showed, if you use a string then you don't get that problem.
You may find it useless and confusing but for others it's useful. And certainly for somebody with a background in C-type languages, the opposite would be confusing.
In matlab, you could use this feature for caesar-type like cyphers for examples:
cypher('A':'Z') = circshift('A':'Z', 13); %build caesar cypher
cypher('THE QUICK BROWN FOX') %use cypher
The feature is used twice above: 1) to build the vector 'ABCD...Z' with the colon operator (if char wasn't treated as a number 'A':'Z' wouldn't work. 2) to index the cyper array.