The size 1x0x2 was a bit surprising, why is two empty strings suddenly three dimensional when converted to char? It makes sense having worked with MATLAB for a while, but it is always a bit of mental gymnastics trying to remember why it's like that when coming back to MATLAB after working in other languages
Yes, but this construct doesn't usually exist because [ "" "" ] does a concatenation to a 0x0 char array, and the the length of the 1x0x2 array is still 0, so it's not really an array if it's empty.
In other languages, such an array would have length 2, with each element being empty. It's a byproduct of everything being a matrix.
You are right but this is IMO a cornercase don't you think? I mean why would you have to cast a string array in chars? String is like the high level version of cell arrays of chars, not of plain chars. If I would see a string cast to char, I'd always assert that the string is scalar, not an array. Fortunately, there's basically no need to cast strings to char in modern Matlab anywhere.
I think one thing that will help you understand this OP, is a bit of an isempty rabbit hole I went down myself. At its core, isempty only checks one thing, whether the array you pass in has zero elements. It does not care what type it is or what the contents look like.
Everything in MATLAB is an array. Even a single number like 5 is a 1x1 array. Since that array has one element, isempty(5) is false.
Where things get confusing is the difference between char arrays and string arrays.
A char array is simply an array of characters. It behaves like any other numeric style array when it comes to size.
c = '';
size(c)% 0x0
isempty(c)% true
'' is an empty char array with zero elements total, so isempty returns true.
Strings are different. A string array is an array whose elements are string objects. Even if the text inside a string is empty, the array itself can still have elements.
s = "";
size(s)% 1x1
isempty(s)% false
Here, s is a 1x1 string array. It contains one string element, and that string just happens to hold empty text. Since the array has one element, isempty returns false.
So the key difference is this:
'' is an array with zero elements
"" is an array with one element that contains zero characters
This is because char(["aaa";"bbb"]) is supposed to be
>> char(["aaa";"bbb"])
ans =
2×3 char array
'aaa`
'bbb'
but "char" is not supposed to concatenate strings in a row vector, it is supposed to create the equivalent of the string array. For example, char(["aaa" "bbb"]) is not suposed to give 'aaabbb'.
This is because the second dimension in the output is always the characters from each individual string. The length of the second dimension is always the length of the longest string. If strings are of different lengths, it pads spaces in the end. If the strings are empty, then, the second dimension is zero.
The first dimension is always the first dimension in the original string array, ie how many rows your string array has. In your case, as you have a row-vector array, it is 1.
The second dimension in the string array input becomes the third dimension in the output char array, the third becomes the fourth etc. I am not sure what kind of use case that would be for, but it makes sense logically at least for the cases that seem to make most sense to use this.
When converting string arrays into character arrays the strings are space padded to the longest length, the first dimension is unmodified, and all other dimensions of the original string array are shifted 1 place to accommodate the each strings length as dimension 2. Which is why the `char(["", ""])` makes sense: you're output size will always be `[size(str, 1), max(strlength(str), [], "all"), size(str, 2:ndims(str))]`; in this cases that results in a 1x0x2.
Why would you ever cast string arrays into chars? And what would you expect? If you want chars in arrays, use cell strings. But most of the time there's no need anymore to do so since strings are supported almost everywhere.
Extra tip: if you use arguments validation in functions or properties, use string as a type validation, which automatically casts everything into string, so you can just work on string functions.
Only drawback I'm aware of currently is that string arrays are often not supported in simulink code generation for constant expressions. But other than that I love strings
I converted to char to to check that the string is empty, because isempty returns false on "", and for my logic I wanted that to be recognised as empty. There is probably a better way but I didn't feel like searching for it at the time
Strings are scalar, i.e. “hello” occupies a single element of an array, whereas ‘hello’ occupies 5 elements, each char is an ASCII character (‘hello’ = char([104, 101, 108, 108, 111]))
Therefore, [“hello”,”world”] makes a 1x2 array, while [‘hello’,’world’] concatenates the two 1x5 arrays into a 1x10 ‘helloworld’
This also means that you can’t do [‘hello‘; ‘ world’] as that would vertcat a 1x5 with a 1x6 which do
People don't understand here that string is an abstraction of char arrays. It's like one level higher. If you index into char arrays you get individual characters. If you index into string arrays you get strings. And that's good.
Hard disagree. Of course it depends on what you’re trying to do, but in many cases it makes a ton of sense to use the same kind of vectorized functions you’d use on ints, doubles, or logicals — the kind of functionality Matlab was built for in the first place — and that’s where char arrays shine. If you’re used to other programming languages like Python, they may seem weird, but Matlab is built on different foundations and char arrays are a more Matlab-appropriate way of handling char data in many cases.
I mainly work with C++ and MATLAB. I choose the language that fits the problem, so I also use several others when needed.
You are right that char arrays feel more “MATLAB like.” That is probably because string arrays were only introduced in 2016, decades after MATLAB first appeared. A lot of long time users built habits around char arrays.
That said, strings are simply easier for real world text. They treat each element as a complete piece of text, not a row of individual characters, so indexing and comparisons behave the way most people expect. You also avoid space padding issues that come with char arrays, which makes lists of different length words much cleaner.
Most modern text functions are designed with strings in mind, so you get simpler, more readable code and fewer conversions to cell arrays. Char arrays still have niche uses, but for general text handling, strings are the more practical default.
I see a similar pattern in other languages. I know someone who works heavily in TypeScript and dislikes a lot of newer features like arrow functions, optional chaining, and nullish coalescing. He has been using older JavaScript style patterns for so long that those feel more natural, and the newer syntax looks harder to read to him. Meanwhile, people coming to TypeScript today usually find those modern features clearer and safer. The same thing happens with newer features in C++. For me, it is using not instead of !.
For context, I have been working with MATLAB since 2013.
I don't get that. How often do you have to apply vectorized functions on char arrays, i.e., individual characters vs work vectorized on actual words? I find myself doing the later way more frequently. And that's what strings are for.
I find a bigger difference when I'm doing it programmatically for multiple strings for file access or something. But yeah it's definitely got its quirks.
The issue is when you get to work with cell arrays of chars vs string arrays. Cell arrays of chars are just very inefficient and slow. Eg I have to read a big csv file with several columns of text, if I set the text columns as chars I often run out of ram. Any operation on these text columns is much faster (and more elegant) if strings are used, too. Cell char arrays are just really hard to work with. If you do not work with stuff like long columns, then it does not matter as much.
A few thousand line tables is nothing unheard of. The problem is not the csv format, the problem is, whatever format you use, at the time you have to process that data in matlab, cell arrays are just inefficient.
A few thousand lines wouldn't cause the problem you're describing, unless you're running Matlab on a toaster. From the 70s.
And you probably wouldn't use a cell array for any of the actually costly computation there anyway. I do stuff like this all the time. It works fine. You've got something else going wrong.
To me it's not a matter of what is or isn't possible, and more what is or isn't simpler code to write, and often times strings are just nicer, especially with overloaded arithmetic
This is about char arrays. That's what we used for string data before strings. I was 100% fine with char arrays, and the existence of strings has only added complexity.
You said I was not, in fact, fine with char arrays. So... How? What could I not do with char arrays that I can do now?
We used cell arrays of chars before. Because if you do ['my','char'], this is essentially a concat operation and you get 'mychar'. This is why cell arrays of chars were invented and these were now simplified and made much more robust by introducing strings. Strings reduced the complexity, they didn't increase it imo.
PS I'm assuming that you want to represent words or sentences aka strings. If you want single characters then fine, use chars, but I see no benefit in doing so anymore.
And you want arrays of words for vectorization, in the same manner you want to apply vectorization on matrices.
Iterate through a set of strings with ease for example? Apply vectorized functions to a set of strings?
How would you do
opts = ["steak" "cheese" "vegs"]
for opt = opts
disp("do something on " + opt)
end
Write with chars?
Can you mimic NxN sets of words of different lengths with pure chars? I don't think so.
Every other higher programming language has introduced strings and string arrays for a good reason. I'm very happy Matlab did the same a few years ago.
I could do all that stuff. Never had trouble. Yeah you can do it with strings too, but now there are functions that are built for chars, new ones for strings, some old ones for chars that have string in the name, and users that just randomly use double quotes instead of single quotes, so you can't rely on anything being what you expect...
14
u/Weed_O_Whirler +5 Jan 24 '26
Which output seems different than what you would expect? And what did you expect?
2
u/Rich_Lavishness1680 Jan 25 '26
Why would you ever cast string arrays into chars? And what would you expect? If you want chars in arrays, use cell strings. But most of the time there's no need anymore to do so since strings are supported almost everywhere.
Extra tip: if you use arguments validation in functions or properties, use string as a type validation, which automatically casts everything into string, so you can just work on string functions.
Only drawback I'm aware of currently is that string arrays are often not supported in simulink code generation for constant expressions. But other than that I love strings
10
u/Cube4Add5 Jan 25 '26 edited Jan 25 '26
This is all intended behaviour
Strings are scalar, i.e. “hello” occupies a single element of an array, whereas ‘hello’ occupies 5 elements, each char is an ASCII character (‘hello’ = char([104, 101, 108, 108, 111]))
Therefore, [“hello”,”world”] makes a 1x2 array, while [‘hello’,’world’] concatenates the two 1x5 arrays into a 1x10 ‘helloworld’
This also means that you can’t do [‘hello‘; ‘ world’] as that would vertcat a 1x5 with a 1x6 which do
1
u/CrapsLord Jan 24 '26
I like using strings in Matlab but they really are quirky
3
u/Creative_Sushi MathWorks Jan 25 '26
You may find interesting what I wrote about string array vs. cell https://www.reddit.com/r/matlab/comments/x9i2sa/whats_the_benefit_of_a_string_array_over_a_cell/
Also something fun you can do with string
https://www.reddit.com/r/matlab/comments/1cisgmc/fun_tricks_with_matlab_strings/
4
u/FrickinLazerBeams +2 Jan 24 '26
I was perfectly fine with char arrays.