After installing and using MATLAB, you can delve into tutorials to enhance your knowledge of MATLAB. Creating strings in MATLAB is straightforward. Refer to the examples below to understand strings in MATLAB.
String Manipulation in MATLAB
Illustration of character strings in MATLAB: Enter the following command after the command prompt:
my_string = 'CodeCrafters Hub'
MATLAB will execute the command and return the result below:
my_string = CodeCrafters Hub
MATLAB considers all variables as arrays, and strings are treated as arrays of characters. Use the whos command to inspect the created variables:
whos
MATLAB will execute the command and return the following result:
You can use conversion functions like uint8 or uint16 to convert characters in a string to numerical codes. The char function converts an integer vector to characters.
Example
Create a script file and enter the code snippet below:
When running the file, it will display the result as follows:
Rectangular character array in MATLAB
String arrays in MATLAB, as mentioned by Mytour, are primarily one-dimensional character arrays. However, if you need to store multiple text data in the program, you can achieve this by creating rectangular character arrays.
The simplest way to create a rectangular character array is by concatenating 2 or more one-dimensional character arrays, either vertically or horizontally as needed.
You can concatenate strings vertically in one of the two ways below:
- Use the MATLAB concatenation operator [] and separate each row with a semicolon (;). Note that in this method, each row must contain the same number of characters. For strings of different lengths, you may need to add space characters if necessary.
- Use the char function. If the strings have different lengths, char adds spaces to shorter strings to make the number of characters in each row equal.
For example:
Create a script file and enter the code snippet below:
When running the file, it will display the result as follows:
To concatenate strings horizontally, you can apply one of the following two methods:
- Use the MATLAB concatenation operator [] and separate the input strings with commas or spaces. This method preserves spaces in the input.
- Use the string concatenation function strcat. This method removes spaces in the input.
For example:
Create a script file and enter the following code:
When running the file, it will display the result as follows:
Combine Strings into Cell Array in MATLAB
In MATLAB, strings in an array must have the same length. To equalize the lengths, a simple way is to use spaces. However, a more efficient way to combine strings is to convert the result array into a cell array.
In MATLAB, a cell array can contain arrays of different sizes and data types. Cell arrays are more flexible in storing strings of varying lengths.
The function cellstr converts a character array in a string into a cell array.
For example:
Create a script file and enter the following code:
When running the file, it will display the result as follows:
String Functions in MATLAB
MATLAB provides a variety of string functions for creating, concatenating, parsing, comparing, and manipulating strings.
Below is a table listing the String functions in MATLAB.
List of functions for storing text in character arrays, combining character arrays:
List of functions identifying parts of a string, finding and replacing substrings:
List of functions comparing strings:
List of functions converting strings to uppercase, lowercase, or removing spaces:
Example:
Below are some examples of the string functions mentioned above:
Example of string formatting function:
Create a script file and enter the code snippet below:
When running the file, it will display the result as follows:
Example of combining strings:
Create a script file and enter the code snippet below:
When running the file, it will display the result as follows:
Example of finding and replacing strings:
Create a script file and enter the code snippet below:
When running the file, it will display the result as follows:
Example of string comparison function:
Create a script file and enter the code snippet below:
When running the file, it will display the result as follows:
The Mytour article just introduced you to character strings (String) in MATLAB. Future Mytour articles will focus on explaining how to write functions and declare arrays in MATLAB. Readers can follow new articles on Mytour for more insights.
In addition, writing and running Script files in MATLAB is straightforward. If you haven't explored it yet, refer to the guide on writing and running Script files in MATLAB here.