Matlab

strcat() in MATLAB

The strcat() function in MATLAB is an effective tool for concatenating strings. It makes it easier to perform a variety of string manipulation operations by allowing you to merge many strings into a single string. The strcat() function in MATLAB is covered in-depth in this article, along with examples of how to use it.

What is strcat() Function in MATLAB

In MATLAB, the strcat() function is used to concatenate strings. It combines multiple strings into a single string by appending them together. The resulting string is formed by joining the input strings in the order they are specified, syntax for strcat() in MATLAB is as follows:

result = strcat(str1, str2, ...)

 

Here, str1, str2, and subsequent arguments represent the strings to be concatenated. The function concatenates the input strings in the order they are specified and returns the concatenated string as the result. Here are some usage examples of the strcat() function to understand how it works:

Example 1: Concatenating Two Strings

str1 = 'Hello';
str2 = 'Sam';
result = [str1, ' ', str2];
disp(result);

 

In this example, the strcat() function combines the strings Hello and Sam to create a single string, Hello Sam:

Example 2: Concatenating Multiple Strings

str1 = 'Welcome';
str2 = 'to';
str3 = 'MATLAB';
result = [str1, ' ', str2, ' ', str3];
disp(result);

 

Here, the strcat() function concatenates the strings “Welcome”, “to”, and “MATLAB” along with space characters (‘ ‘) in between to form the string “Welcome to MATLAB”.

Example 3: Concatenating Numeric Values as Strings

num1 = 10;
num2 = 20;
result = strcat('The sum is: ', num2str(num1 + num2));
disp(result);

 

In this example, the strcat() function combines the string “The sum is:” with the numeric sum of num1 and num2 after converting it to a string using the num2str() function.

Example 4: Concatenating Cell Array of Strings

cellArray = {'Welcome', 'To', 'MATLAB'};
result = strjoin(cellArray, ' ');
disp(result);

 

Here, the strcat() function concatenates the strings from the cell array cellArray using the {:} syntax, resulting in the single string “Welcome To MATLAB”:

Conclusion

The strcat() function in MATLAB provides a convenient way to concatenate strings, allowing for versatile string manipulation. By understanding the syntax and usage examples, you can effectively utilize strcat() to combine strings, whether it’s concatenating two strings, merging multiple strings with separators, converting numeric values to strings, or concatenating strings from a cell array.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.