The strcat function is widely used and is one of the standard libraries of the most widely used programming languages in the world. Its inputs, outputs, and execution mode are the same for all of them.

This tutorial contains practical examples that show how you can use this function with different types of inputs and outputs. It also explains some of the most common errors and their corresponding error messages, and how you can fix them to make programming easier.

## Matlab strcat Syntax

**s = strcat(s1….sn)**

## Matlab strcat Description and Examples

The strcat() function concatenates the character strings sent in the input arguments and returns them in “s” to form a single concatenated string. This function performs horizontal concatenation only. The input arguments accepted by this function can be character strings, cell arrays of character vectors, character vectors, or arrays of character strings. The data type supported by the strcat() is char, cell, and international character set compatible strings. In cases where the inputs are arrays of ASCII characters, strcat() strips trailing whitespace and escape characters, as well as vertical tabs and fonts. This applies only to char arrays. In cases where the inputs are cell arrays and string arrays, strcat() does not remove these characters. The strcat() function follows the following input and output rules:

- If any input is an array of strings, the strcat() function will return an array of strings as a result.
- If any input is a cell array and none is a string array, strcat() will return a cell array of character vectors.
- If all the inputs are character arrays, the result will have the same format.

## How to Use the strcat() Function to Concatenate Two Strings in MATLAB

In the following example, we will see the simplest way to concatenate two strings with the strcat() function.

The strcat function will return:

Which is the same as:

s2 = ' World 2';

s = strcat(s1, s2);

The strcat function will return:

## How to Concatenate Two Cell Arrays of Character Vectors with MATLAB’s strcat() Function

Now, let us see how you can concatenate two vector cell arrays using the strcat() function. To do this, we will create the arrays s1 and s2 with the different days of the week.

s2 = {'Friday ', ' Saturday'};

st = strcat (s1, s2)

As a result, the strcat() function returns in “st” the concatenation as follows:

This example shows the concatenation sequence on vector cell arrays with the strcat() function.

s2 = {' I am ',' MATLAB '};

st = strcat (s1, s2)

The strcat function will return:

In this case, the cell arrays of the character vectors must have the same size to be sent as input arguments in the strcat() function, otherwise the following error message is returned:

“Error using **cell/strcat** (line n) All nonscalar inputs must be the same size.”

Let us look at this with an example where the cell arrays of the character vectors are of different sizes.

s2 = {' I am ',' MATLAB ', ' !! ' };

st = strcat (s1, s2)

## How to Concatenate String Matrix with MATLAB’s strcat() Function

The following example shows how to use the strcat() function to concatenate two string arrays. For this purpose, we create the matrices “m1” and “m2” from 2×2 strings.

m2 = [" world", " MATLAB" ];

str = strcat (m1, m2)

As a result of the concatenation of the two matrix strcat() returns:

1×4 string array

"Hello world" " I am MATLAB"

## How to Concatenate Two String Arrays with the strcat() Function in MATLAB

Now, we will see how to concatenate arrays of strings. To do this, we create the arrays “a1” and “a2” with strings and concatenate them with the strcat() function. The result in “st” will be the horizontal concatenation of these two strings.

a2 = [' I am', ' Matlab'];

st = strcat(a1, a2)

The result in “st” will be the horizontal concatenation of these two strings.

Note that in this case strcat() does not concatenate element 1 of “a1” with element 1 of “a2” and element 2 of “a1” with element 2 of “a2”, as is the case with cell arrays. In this case, all elements of “a1” are concatenated and then concatenated with all elements of “a2”, as shown in the following example:

a2 = [' 3', ' 4'];

st = strcat (a1, a2)

st = 1 2 3 4.

While for cell arrays of character vectors for example, the concatenation is done element 1 of “s1” with element 1 of “s2” and so on.

s2 = {' 3 ',' 4 '};

st = strcat (s1, s2)

st = {' 1 3 '} {' 2 4 '}

## How to Concatenate Strings or Character Vectors with the “+” Operator

It is also possible to compare or concatenate strings using operators. The following example shows how to concatenate strings using the “+” operator and store the result in “st”, as we did with the strcat() function.

s2 = "Matlab"

str = s1 + s2

In this case, the operation will result in the concatenation of “s1” and “s2” as seen below.

## Conclusion

In this article, we have explained how to use one of MATLAB basic string concatenation functions with the strcat() function. To make programming easier for you, I have included some practical examples and images that show how to use these functions and the most common errors with their respective messages and solutions. In addition, the options that MATLAB provides for string concatenation with operators are briefly explained. The argument types supported by this function and the accepted data type have also been explained in more detail. We hope you have found this MATLAB article useful. Check out other Linux Hint articles for more tips and information.