What are strings in MATLAB?

MATLAB supports the use of strings and an array of strings. MATLAB stores strings as short pieces of text as character vectors, e.g., str = "Hello World".

MATLAB supports the use of both individual strings or an array of strings. MATLAB string array may be one-dimensional or multidimensional.

// Indvidual string element
str = "Hello World" 

// Array of strings 1 dimensional
array_of_str = [ "hello" "I" "am" "MATLAB" "Array"]

// Array of strings 2 dimensional
array_of_str = [ "hello" "I" "am"; "MATLAB" "Array" "2D"]

Let’s look at some common string functions.

Find the length of a string

The strlength() function in MATLAB finds the length of the string. This function can be used to find the length of an individual string as well as an array of strings.

In the case of an individual string, the strlength() function returns the length of the string as a numeric data type.

In the case of an array of strings, the strlength() function returns a numeric array with the length of strings on respective array positions. This is further explained in the example below:

Note: MATLAB will count empty spaces (if they are part of the string) in the strlength() function.

// Find length of individual string
str = "Hello"
strlength(str)
// Answer will be 5

// Find length of individual string with empty space
str = "Hello World"
strlength(str)
// Answer will be 11 as it counts 5 for Hello, 5 for World, and 1 for empty space

// Find the length of an array of strings
str_array = [ "hello" "I" "am"; "MATLAB" "Array" "2D"]
strlength(str_array)

//The strlength() function will return a 2x3 array:
// [ 5     1     2
//   6     5     2 ]
// Each respective array entry has length of string in original array

Remove a keyword from a string

The erase() function in MATLAB allows us to remove a keyword from a string. For example, we can remove “!” from “Hello!” using the erase() function. This also works with string arrays. See the example below for more details:

str = "Hello! World! I am MATLAB!"
// we can remove all instances of ! from the above string using the erase() function.
erase(str,"!")
// answer after erase will be "Hello World I am MATLAB"

str_array = [ "hello" "am"; "array" "arr"]
erase(str_array,"a")
//This will remove all "a" from all strings of the above array, resulting in:
//   [ "hello"    "m" 
//     "rry"      "rr" ]

Similarly, we can remove any keyword or a set of words from a string using the erase() function.

Convert the string to lowercase

MATLAB provides a lower() function that allows us to convert upper case letters of a string to lower case. For example, we can convert "HELLO" to "hello" using the lower function. This also works with string arrays. See the example below for more details:

// For individual strings
str = "HellO WorlD I AM MATLAB"
lower(str)
// Answer will be, hello world i am matlab.

//For arrays
str_array = ["HELLO" "i" "AM"]
lower(str_array)
// the function converts all array items to lower case.
// answer for above example will be:
// [ "hello"    "i"    "am" ]

Split a string

MATLAB provides a split() function that converts long strings into individual string items. For example, we can convert "HELLO World I am MATLAB" (which is a single string) to "HELLO, World, I, am, MATLAB" (where HELLO, World, I, am, MATLAB are five distinct strings) using the split function. See the example for more details. The split() function returns an array of elements of the size, nx1. Here, n is the number of unique elements made after the split.

For string arrays, the split function also works similarly.

Delimiter is the argument on which we have to split. It could be an empty space, a keyword like “!”, or any other input.

Note: We can split on any delimiter we want. MATLAB splits on empty space if no delimiter is specified.

// General form of split
split(string_input, delimiter)
// default delimiter for matlab is empty string.
// split on default delimiter i.e. empty space
str = "hello world i am matlab"
split(str)
//answer will be a 5x1 array 
// [ "hello"
//   "world"
//   "i"
//   "am"
//   "matlab" ]

// split on a specified delimiter for example say "o"
str = "hello world i am matlab"
split(str, "o")
// answer will be a 3x1 array
//[  "hell"
//   " w"
//   "rld i am matlab"]


// Split an array
str_arr = ["I am"; "MATLAB online"]
split(str_arr)
// answer will be 2x1 array
// [ "I"         "am"    
//   "MATLAB"    "online" ]

Free Resources