Why does MATLAB differentiate between strings and character array data types?
11 views (last 30 days)
I am curious why this language makes a distinction between strings and arrays of characters. In python eg these are effectively the same data type and array opperations can be performed on strings, which are just treated as an ordered series of characters. The distinction seems arbitrary and annoying to me since after all, words and phrases are, logically speaking just an ordered series of letters, so why not always treat them as such and eliminate an unnecessary data type and the type conversion required?
Rik on 12 Aug 2022
Edited: Rik on 12 Aug 2022
The string data type was introduced a few years ago (in R2016b) to introduce new features. Mathworks chose to use a new syntax to define strings, instead of extending the char data type.
Note that char is not really the equivalent of string: a cell array of char vectors is. You can easily convert to that with the cellstr function. I you were really determined, you could make a custom class implementing all features string offers.
When you realize string is an extension of cellstr, it makes sense they left char alone.
The main confusing thing is that string, char vector, and cellstr were used interchangeablely before the introduction of the string data type, which is why most properties are still called string, even if they are actually char vectors.