A question about text string concatenation

12 views (last 30 days)
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 Comment
Stephen23
Stephen23 on 12 Jun 2025
Edited: Stephen23 on 12 Jun 2025
"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).

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 12 Jun 2025
That would work as you expected if Cruise_ID were a char vector. In that case, the concatenation would make a longer char vector, one where each element is a single character.
Cruise_ID = 'W2016'
Cruise_ID = 'W2016'
Cruise_N = 1;
a = [Cruise_ID, ' [No. ', num2str(Cruise_N,'%.0f'), ']']
a = 'W2016 [No. 1]'
But it's not. It's a string array.
Cruise_IDs = string(Cruise_ID)
Cruise_IDs = "W2016"
a = [Cruise_IDs, ' [No. ', num2str(Cruise_N,'%.0f'), ']']
a = 1×4 string array
"W2016" " [No. " "1" "]"
In that case, MATLAB will convert the char vectors in your command into strings and then concatenate the strings together. What you receive would be a string array where each element is one of the sub-strings that you concatenated together.
There are a few ways to always make a single piece of text. You could make at least one piece of the "fixed text" into a string and use the + operator. This, like concatenation, would convert the char vectors into strings and concatenate them together. It won't do what you expect if all the things you're "adding" together are char vectors, though. In this first example the "fixed text" segment " [No. " is a string so Cruise_ID, the output of num2str, and the ']' are all converted to strings before + adds them together.
a = Cruise_ID + " [No. " + num2str(Cruise_N,'%.0f') + ']'
a = "W2016 [No. 1]"
You could use strcat. Note a subtle difference between this and the others: there's no space after the No. piece, and this is expected behavior. strcat trims trailing whitespace in its inputs before concatenting them together.
a_char = strcat(Cruise_ID, ' [No. ', num2str(Cruise_N,'%.0f'), ']')
a_char = 'W2016 [No.1]'
a_string = strcat(Cruise_IDs, ' [No. ', num2str(Cruise_N,'%.0f'), ']')
a_string = "W2016 [No.1]"
You could use sprintf. Note that the type of the output (char or string) depends on the type of the format specifier.
a_charin_charout = sprintf('%s [No. %.0f]', Cruise_ID, Cruise_N)
a_charin_charout = 'W2016 [No. 1]'
a_stringin_charout = sprintf('%s [No. %.0f]', Cruise_IDs, Cruise_N)
a_stringin_charout = 'W2016 [No. 1]'
a_charin_stringout = sprintf("%s [No. %.0f]", Cruise_ID, Cruise_N)
a_charin_stringout = "W2016 [No. 1]"
a_stringin_stringout = sprintf("%s [No. %.0f]", Cruise_IDs, Cruise_N)
a_stringin_stringout = "W2016 [No. 1]"

More Answers (2)

Matt J
Matt J on 12 Jun 2025
Edited: Matt J on 12 Jun 2025
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 = 1×4 string array
"W2016" " [No. " "1" "]"
a = strjoin(a)
a = "W2016 [No. 1 ]"

Benjamin Kraus
Benjamin Kraus on 12 Jun 2025
Edited: Benjamin Kraus on 12 Jun 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
a = 'helloworld'
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
a = 1×2 string array
"hello" "world"
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.
a = 1×2 string array
"hello" "world"
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.
a = 1×2 string array
"hello" "world"
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.
As @Matt J points out, the easiest solution is to use strjoin instead:
a = strjoin(Cruise_ID(Ind3), ' [No. ', num2str(Cruise_N(Ind3),'%.0f'), ']');
  1 Comment
Leon
Leon on 12 Jun 2025
Many thanks. You all are wonderful teachers explaining these trivial differences.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!