Matlab append method can be used to append a string and add another string to it. This method is very handy when combining multiple strings and getting a single string as the output.
For example, if we take 2 strings, ‘First Name’ and ‘Last Name’, as inputs from the user, later there might be a possibility that we need to combine these 2 strings and get a single string which will give us the full name. This is where we use the append method in Matlab.
Syntax:
- NewString = append (string1, string2, …. stringN) is used to combine various strings in Matlab. The strings will be combined in the same order as they are passed as arguments.
- As it is evident from the syntax, we can combine multiple strings using the append method.
Examples of Matlab Append
Given below shows how to combine strings in Matlab using the append method:
Example 1:
In this example, we will use the append method to combine 2 strings.
The steps to be followed for this example are:
- Initialize the strings that are to be combined.
- Pass the above strings as arguments to the append function.
Code:
- string1 = “MATLAB”
[Initializing the 1st string]
- string2 = “Append”
[Initializing the 2nd string]
- NewString = append(string1, string2)
[Passing the above 2 strings to the append method. Please note that the order of arguments must be the same in which we want the strings to be combined]
This is how our input and output will look like in the Matlab command window:
Input:

Output:

As we can see in the output, we have obtained a string that combines string1 and string2.
In the above example, we saw that the 2 input strings were combined, but there was no space between them in the final output.
Next, we will see how to add a space or any special character between 2 strings.
Example 2:
In this example, we will use the append method to combine 2 strings with a space between them.
The steps to be followed for this example are:
- Initialize the strings that are to be combined.
- Add an extra space at the beginning of the 2nd string.
- Pass the above strings as arguments to the append function.
Code:
- string1 = “MATLAB”
[Initializing the 1st string]
- string2 = “ Append”
[Initializing the 2nd string. Please note that we have added an extra space at the beginning of string2. This is done to get a space in the combined string]
- NewString = append(string1, string2)
[Passing the above 2 strings to the append method. Please note that the order of arguments must be the same in which we want the strings to be combined]
This is how our input and output will look like in the Matlab command window:
Input:

Output:

As we can see in the output, we have obtained a string that combines string1 and string2. If we compare this output with the output in example 1, we will notice that now we have a space between two strings.
In the above examples, we combined 2 strings; next, we will see to combine more than 2 strings using the append method.
Example 3:
In this example, we will use the append method to combine multiple strings with a space between them.
The steps to be followed for this example are:
- Initialize the strings that are to be combined.
- Add an extra space at the beginning of all the strings, except the first one.
- Pass the above strings as arguments to the append function.
Code:
- string1 = “Let”
[Initializing the 1st string]
- string2 = “ us”
[Initializing the 2nd string. Please note that we have added an extra space at the beginning of string2. This is done to get a space in the combined string]
- string3 = “ learn”
[Initializing the 3rd string. Again, there is an extra space]
- string4 = “ MATLAB”
[Initializing the 4th string]
- NewString = append(string1, string2, string3, string4)
[Passing the above 4 strings to the append method. Please note that the order of arguments must be the same in which we want the strings to be combined]
This is how our input and output will look like in the Matlab command window:
Input:

Output:

As we can see in the output, we have obtained a string that combines all the input strings.
Example 4:
In this example, we will use the append method to combine string arrays of vectors. These character vectors are combined element by element.
The steps to be followed for this example are:
- Initialize the string arrays that are to be combined.
- Pass the above string arrays as arguments to the append function.
Code:
- Names = [“John” “Sam” “Mark” “Vince”]
[Initializing the 1st string array]
- Department = [“ HR” “ Finance” “ Operations” “ Marketing”
[Initializing the 2nd string array]
- NewString = append(Names, Department)
[Passing the above 2 string arrays to the append method. Please note that the order of arguments must be the same in which we want the arrays to be combined]
This is how our input and output will look like in the Matlab command window:
Input:

Output:

As we can see in the output, we have obtained the combination of 2 string arrays.