Updated March 8, 2023
Introduction to Matlab Append
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”
- string2 = “Append”
- NewString = append(string1, string2)
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”
- string2 = “ Append”
- NewString = append(string1, string2)
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”
- string2 = “ us”
- string3 = “ learn”
- string4 = “ MATLAB”
- NewString = append(string1, string2, string3, string4)
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”]
- Department = [“ HR” “ Finance” “ Operations” “ Marketing”
- NewString = append(Names, Department)
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.
Conclusion
Append is used in Matlab to combine 2 or more strings. We can also combine string arrays using the append method. Special characters like ‘space’ can be added if need be.
Recommended Articles
This is a guide to Matlab Append. Here we discuss the introduction to Matlab Append along with examples for better understanding. You may also have a look at the following articles to learn more –