A question about text string concatenation
Afficher commentaires plus anciens
Below is my code to concatenate several text strings together. Why doesn't it work? When I used disp(a), it shows them as separate strings. I thought the square brackets would enable me to concatenate them together. What am I missing?
a = [Cruise_ID(Ind3), ' [No. ', num2str(Cruise_N(Ind3),'%.0f'), ']'];
disp(a)
"W2016" " [No. " "1" "]"
Here is the error message:
Error using matlab.ui.control.EditField/set.Value (line 115)
'Value' must be a character vector or a string scalar.
1 commentaire
"Why doesn't it work?"
It does work: because Cruise_ID(Ind3) is a scalar string you told MATLAB to concatenate 4 scalar string arrays into a larger string array, implicitly converting any character vectors and numeric values to string before doing so. And so you get a 1x4 string array, which is the expected and documented output when concatenating strings with other strings (or most anything else).
This is explained in detail in the FAQ:
"I thought the square brackets would enable me to concatenate them together.
They do... concatenate string arrays (not character vectors), because that is what you are concatenating.
"What am I missing?"
The fact that string arrays are container arrays.
The fact that scalar strings are not the same as character vectors.
The fact that scalar strings have size 1x1 (and not a size that depends on how many characters they contain).
The fact that concatenating string arrays concatenates string arrays (and not their content).
Réponse acceptée
Plus de réponses (2)
Using [] are for concatenating char vectors, but some of your variables are apparently strings. But you can remedy this with strjoin,
a=["W2016" " [No. " "1" "]"]
a = strjoin(a)
1 commentaire
Leon
le 12 Juin 2025
Benjamin Kraus
le 12 Juin 2025
Modifié(e) : Benjamin Kraus
le 12 Juin 2025
The issue you are having is that Cruise_ID(Ind3) is a scalar string and not a character row vector.
Concatenation works different for character row vectors (the old way of doing text in MATLAB) and string (the new way of doing text in MATLAB). For example:
Concatenation of character row vectors (created with single quotes) will create a single new character row vector:
a = ['hello', 'world'] % This creates a single 13 character row vector
Concatenation of strings (created with double quotes) will create string vectors:
a = ["hello", "world"] % This creates a 2 element string vector with two separate words
If you mix scalar strings with character row vectors, concatenation will defer to the strings, so the code below will create a 2 element string vector (note a mix of a string and a character row vector):
a = ["hello", 'world'] % This is equivalent to the previous.
The order doesn't matter, so a character row vector and a string will behave the same as a string and character row vector:
a = ['hello', "world"] % This is equivalent to the previous.
This behavior might be more clear if you knew that using [] is equivalent to calling horzcat (for "horizontal concatenation").
% These two are equivalent
a = ["hello", 'world'];
a = horzcat("hello", 'world');
Now on to your actual code:
a = [Cruise_ID(Ind3), ' [No. ', num2str(Cruise_N(Ind3),'%.0f'), ']'];
That is equivalent to:
a = horzcat(Cruise_ID(Ind3), ' [No. ', num2str(Cruise_N(Ind3),'%.0f'), ']')
When evaluating that statement, MATLAB looks at each argument to determine the "dominant class". In this case, assuming Cruise_ID(Ind3) is a string scalar, the dominant class is "string":
input1 = Cruise_ID(Ind3) % string scalar
input2 = ' [No. ' % character row vector
input3 = num2str(Cruise_N(Ind3),'%.0f') % character row vector
input4 = ']' % character row vector
a = horzcat(input1, input2, input3, input4); % Result will be a string vector with four elements.
a = [input1, input2, input3, input4; % Equivalent to the previous line.
Because one of the inputs is a string, the resulting output will be a string vector instead of a character row vector. This explains why disp(a) shows a 4 element string vector.
The Value property of a EditField only accepts either character row vectors or string scalars, so a 4 element string vector is invalid.
a = strjoin(Cruise_ID(Ind3), ' [No. ', num2str(Cruise_N(Ind3),'%.0f'), ']');
1 commentaire
Leon
le 12 Juin 2025
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!