HOWTO · MATLAB

How to Concatenate String in MATLAB

Learn how to combine MATLAB text with strcat(), concatenate compatible arrays, add separators, and choose between strcat() and + for string arrays or character vectors.

On this page

Use strcat() to combine text values in MATLAB. It also works element by element with compatible arrays, so you can create several labeled values in one call. Choose the input type deliberately: MATLAB string arrays and character vectors do not behave identically with + or with whitespace.

Concatenate Text with strcat()

Call strcat(value1, value2, ...) to append text from left to right. For character-vector inputs, strcat() removes trailing space characters from each input before joining them. That behavior can be useful when padding is accidental, but it means trailing spaces are not a reliable way to separate words. Add a separator explicitly when it matters.

Run this example:

hello = 'Hello, ';
name = 'Alice';
result = strcat(hello, name, '!');
fprintf('%s\n', result);
Hello,Alice!

The inputs are character vectors, and the result is a character vector. For MATLAB string scalars, use double quotes, as in strcat("Hello, ", "Alice!"); this returns a string scalar. Keep the string and character-vector forms clear when adapting examples, especially when combining values from different sources.

Concatenate Arrays with strcat()

When array inputs have compatible sizes, strcat() combines corresponding elements and returns an array with the corresponding shape. A scalar text value can be combined with each element of an array. For example, with a string array names = ["Alice", "Bob", "Charlie"], strcat("Hello, ", names) produces one greeting for each name. MATLAB also supports cell arrays of character vectors, such as names = {'Alice', 'Bob', 'Charlie'}; use cell-array syntax consistently when the surrounding code expects a cell array. Array dimensions must be compatible for the operation; if the inputs cannot be paired according to MATLAB’s array rules, adjust their shapes or combine scalar values instead. A cell array of character vectors and a string array are distinct types, so preserve the representation your later code expects rather than assuming they are interchangeable.

Add Separators and Handle Whitespace

strcat() does not insert punctuation or a word separator for you. With character vectors, it also removes trailing space characters from each input, so a separate ' ' argument is trimmed and does not create a visible gap. For a regular space between character vectors, put the separator in a format string with sprintf():

first = 'Ada';
last = 'Lovelace';
fullName = sprintf('%s %s', first, last);
fprintf('%s\n', fullName);

This prints the two names with one space between them. For MATLAB string scalars, strcat("Ada", " ", "Lovelace") preserves the explicit string separator. If a separator should appear between every element of an array, join() is designed for that task and accepts a delimiter; strcat() instead performs corresponding-element concatenation. Leading whitespace is not the same as trailing whitespace, so inspect the actual input values when formatting still looks unexpected.

Choose Between strcat() and +

Use strcat() when you want its text-concatenation behavior for character vectors or string arrays. The + operator concatenates MATLAB string arrays, so it can be concise when both operands are strings. Character vectors use + as numeric addition when their character codes are converted to numbers; it does not concatenate their visible text. Convert character vectors to strings before using +, or use strcat() for character-vector concatenation.

For example, "Good " + "morning" combines two string scalars into "Good morning". By contrast, 'A' + 'B' adds the character codes element by element; because both character vectors have one element, their dimensions are compatible, and the result is the numeric value 131 (65 + 66). Do not infer the result from quote marks alone if values may have been converted earlier in the program: check whether each variable is a string array, character vector, or cell array. That type distinction is the main guide for choosing the operator and predicting the result.