Why does MATLAB differentiate between strings and character array data types?
Afficher commentaires plus anciens
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?
1 commentaire
"In python eg these are effectively the same data type ... "
No, they are not. The approximate equivalent of MATLAB's string array (or even a cell array of character vectors) would be in Python an iterable container of strings, e.g. a list or tuple of strings. Not the same thing at all.
Python's str type is a iterable vector of characters, something a bit like a char row vector in MATLAB. However instances of the str type immutable, thus the ability to manipulate character codes in arrays like in MATLAB does not exist in Python.
"The distinction seems arbitrary and annoying to me since after all, words and phrases are, logically speaking just an ordered series of letters..."
Not all text consists of "words and phrases". I often perform operations on character arrays using basic array operations (i.e. indexing, arithmetic, permutation, etc) to manipulate the character codes themselves, which would be far more complex if only the string class was available. In other situations having an "atomic" string is more useful. I appreciate having the choice between manipulating characters vs strings, for me this is a very useful distinction.
Assuming that your use case of "words and phrases" applies to all other users is unlikely to help understand why other users might find both of those classes useful. One of the neat things about MATLAB is that numeric/char/logical arrays are contiguously stored in memory, and some users appreciate the ability to manipulate such arrays via a high-level language. Understanding the fundamental differences between char and string arrays would go a long way to appreciating when they can be used effectively.
Réponse acceptée
Plus de réponses (1)
Bruno Luong
le 12 Août 2022
0 votes
strings is introduced recently and the behavior is not the same than char array, an historic class. So they create a new class to ensure backward compatibility.
1 commentaire
Rik
le 12 Août 2022
R2016b, so not even that recent anymore ;)
Catégories
En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!