EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial String Array in Matlab
 

String Array in Matlab

Priya Pedamkar
Article byPriya Pedamkar

String Array in Matlab

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:

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

String Array in Matlab1

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:

String Array in Matlab2

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:

String Array in Matlab3

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:

String Array in Matlab4

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:

String Array in Matlab5

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:

String Array in Matlab6

To find the number of characters in a string, we use strlength () function:

Code:

C = "Two and Two make four"
L = strlength(sr)

Output:

strlength () function

Example #3

We can also convert a numeric array into string array:

Code:

N = [ 56 78 89 43 45] sr = string (N)

Output:

convert a numeric array

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:

numeric array

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:

rearrange the strings

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:

first element

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:

two strings into one string

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 –

  1. Matlab Figure
  2. Matlab Legend
  3. Reshape in Matlab
  4. Arrays in Matlab

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW