Introduction to String Array in Matlab
String Array in Matlab, an array is used to store the elements that are of the same data type. We can store numbers or strings in an array. The elements in an array can be accessed by an index number and it starts from 0. The size of an array once defined cannot be changed and different operations can be performed using an array. Arrays are declared by referencing it to a variable.
String Array Declaration in Matlab: An array that consists of a string or list of words is known as a string array. It can have any length and if the string array has one element then it is known as string scalar. There are different functions that can be performed using a string array. String arrays can be declared by using the below expressions:
st = “Smart, Grid”
They can also be declared using double quotes or we can add a string in the array using square brackets like:
Code:
st= [ "Tiger"," Lion"," Cat";
" Deer"," Zebra", "Hippo"]
Output:
Explanation: Here st is a string array.
In the above array, we have declared a string array using square brackets and the output is 2*3 array of strings. If we want to convert any data type into a string, then we can do it by using the “string” keyword. Please find the below functions to convert input array into string data type:
st = string(B): This is used to convert the input of any data type into a string data type.
st = string(D): This is used to convert the input of date data type to string data type. Date can be calendar days, duration, or date.
st = string (D, format): This is used to convert the input of date data type to string data type as specified in the format of the date.
Examples to Implement String Array in Matlab
There are many functions that can be performed using a string array. Please find the below list of functions with examples:
Example #1
ar = “Jack and Jenny !pursued !pursued!Engineering”
To eliminate the special character between the strings we can use erase function which will eliminate “!” symbol from the string.
Code:
ar = "Jack and Jenny !pursued !pursued!Engineering"
ar = erase(ar,”!”)
Output:
To convert the string into lowercase characters we can use the lower function.
Code:
ar = "Jack and Jenny !pursued !pursued!Engineering"
ar = lower(ar)
Output:
This following string contains only lowercase letters. Next, to split the string, we use the split function. Split function breaks the string whenever there is space between the string and consider it as a single word.
Code:
ar = "Jack and Jenny !pursued !pursued!Engineering"
ar = split(ar)
Output:
This function splits the array whenever there is space and it results in a 6* 1 string array. We can also find the unique words in the array by using a unique function.
Code:
ar = "Jack and Jenny !pursued !pursued!Engineering"
ar = unique(ar)
Output:
We can convert character, numeric array to string array by using the “string” function. Please find the below examples:
Example #2
C is a character vector and it can be converted into a string by using string function.
Code:
C = "Two and Two make four"
sr = string (C)
Output:
To find the number of characters in a string, we use strlength () function:
Code:
C = "Two and Two make four"
L = strlength(sr)
Output:
Example #3
We can also convert a numeric array into string array:
Code:
N = [ 56 78 89 43 45]
sr = string (N)
Output:
Example #4
To convert the string array into a numeric array, we use a double function in Matlab.
Code:
sr = ["166","6.1412","2.1e-3"]
N= double(sr)
Output:
How to access the elements of String Array?
The elements in an array can be accessed by using an index number. The index number starts from zero in an array. To add and rearrange the strings in an array we can use plus operator to add strings, join, split, sort functions to rearrange the strings.
Code:
st= [ "Tiger","Lion","Cat";
"Deer","Zebra", "Hippo"]
st (2, :)
Output:
This above function displays all the elements of the second row. To access the first element of the first row we can use:
Code:
st= [ "Tiger","Lion","Cat";
"Deer","Zebra", "Hippo"]
st (1,1); "Tiger"
Output:
To concatenate both strings into one string array, we can use the “+” operator.
Code:
sr1 = ["Tiger","Lion","Cat"];
sr2 = ["Deer","Zebra", "Hippo"];
sr = sr1+sr2
Output:
Conclusion
String arrays and its functionalities have been introduced in Matlab in its recent version. It provides many functions that can be used to manipulate the strings in an array. Different data types like character, numeric, and date can be converted into string datatype in Matlab. We can also create multiple string arrays and manipulate them according to the business requirements.
Recommended Articles
This is a guide to String Array in Matlab. Here we discuss an introduction to String Array in Matlab, working and programming examples. You can also go through our other related articles to learn more –