Clarification: Array of strings vs cell array of character vectors
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Bryan Wilson
am 1 Nov. 2016
Kommentiert: Walter Roberson
am 8 Apr. 2025
I'm beginning to code a new project. I'll have multi-tiered structures to work with, and some of the fields will have labels and comments on certain data entries. I can put these in as arrays of strings OR as cell arrays of character vectors.
comment1 = strings('hello','world','!'); %3x1 string
%vs
comment2 = cellstr(['hello','world','! ']); %3x1 cell
Do you find one is easier to work with than another? I'm leaning towards arrays of strings. It's easier to set up at least.
3 Kommentare
Stephen23
am 8 Apr. 2025
Bearbeitet: Stephen23
am 8 Apr. 2025
"Likely it's even changing over time."
Cell arrays have not really changed since they were introduced, neither have character vectors.
"because '[]' is making concatenation of the cellarays inside to a single cell array,"
Nope, there are absolutely no cell arrays involved in that concatenation, only character vectors. This code:
['hello','world','! ']
concatenates three character vectors into one character vector. Cell arrays have absolutely nothing to do with that concatenation, so your statement mixes up completely different data types.
Just for info...
Walter Roberson
am 8 Apr. 2025
At one point, character vectors changed internal representation to be utf16 instead of UCS-2.
... I still struggle to find proof that it uses utf16 instead of UCS-2. Sure seems like USC-2 to me.
Akzeptierte Antwort
James Tursa
am 1 Nov. 2016
Bearbeitet: James Tursa
am 1 Nov. 2016
Generally, if you will be accessing the individual strings downstream in your code, use a cell array of strings. Many MATLAB functions are already coded to handle these naturally. E.g.,
comments = {'hello','world','!'};
If you also need to concatenate them later on you can always do this:
comment = [comments{:}];
Weitere Antworten (1)
Walter Roberson
am 1 Nov. 2016
The string() data type is very new. I am still trying to figure out what it is good for.
It does add a layer of object transparency, allowing you to say
A(K) = B
without having to know that you instead need to do
A{K} = B
because the content happens to be a character vector. That is good over the long term for object oriented processing.
But in practical terms, the only benefit I have seen to date is that finally we can answer those questions where a student is asked to store a character vector into "a" location. Like the assignments where the student has to write a function that accepts a matrix and returns a matrix of the same size in which each entry of the output matrix is either 'odd' or 'even'. I am convinced that the people who make up the homework assignments do not know much about MATLAB.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!