Why does implicit conversion happen from 'char' to 'string' when concatenating?

4 visualizzazioni (ultimi 30 giorni)
Concatenating char arrays results in a new char array, as expected:
>> ['a', 'b', 'c']
ans =
However, if any element of this array is a string, there is an implicit conversion of all the char arrays to strings, and the behavior is quite different:
>> ['a', "b", 'c']
ans =
1×3 string array
"a" "b" "c"
What is the reason for this?

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 1 Dic 2022
In concatenation operations, the dominant object determines the class of the resulting array, according to the following documentation:
Therefore, when the array contains both 'char' and 'string', the concatenation results in a 'string' array.
The following documentation includes other related examples to demonstrate this idea:

Più risposte (0)


Scopri di più su Characters and Strings in Help Center e File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by