MATLAB Strings

You can address text in MATLAB utilizing string exhibits. Every component of a string exhibit stores a grouping of characters. The arrangements can have various lengths without cushioning, for example, "yes" and "no". A string exhibit that has just a single component is likewise called a string scalar.

You can list into, reshape, and connect string exhibits utilizing standard cluster tasks, and you can add text to them utilizing the + administrator. Assuming a string exhibit addresses numbers, you can change it's anything but a numeric cluster utilizing the twofold capacity.

To Create a String in Matlab

You can create a string placed a text in double quotes as shown below:

Str1 = "world, people"
Str1=
"world, people"

String array

We are creating a string array, we are concatenating string scalars like numbers can be concatenated with a numeric array, using square brackets.

Str1 = ["M" "Gem" "Apo";
"Sky" "Sky B" "IyS"]

Output:

Str1 = 
  2×3 string array
    "M"      "Gem"      "Apo"
    "Sky"    "Sky B"    "IyS"

We have used MATLAB online to execute this code and obtain the result as we can in this screenshot.

Matlab Strings

String Functions in MATLAB

MATLAB gives flexibility by providing many string functions for manipulating strings, creating, combining strings, modify strings, to blank, to copy, comparing and parsing strings.

String functions in MATLAB -

Function nameDescription
isspaceFor Array elements that are space characters
isstrpropTo Determine string category
sscanfTo Read from string formatted data
deblankTo Strip from end of string to trailing blanks
strtrimTo Remove white space from string i.e leading and trailing
charTo Represents character array
iscellstrTo convert cell array of strings
isletterTo check alphabetical letters
isspaceTo check space characters
isstrpropTo Determine category of string
strcatTo horizontally Concatenate the strings
strjoinFor Joining strings into single string in cell array
ischarTo check whether item is character array
isletterTo Array elements that are alphabetic letters
strfindTo Find one string within another
strrepTo replace substring
strsplitTo Split string at delimiter
strtokTo Selected parts of string
ischarTo check item is character array
sprintf To Format data into string
lowerTo Convert string to lowercase
blanks To represents blank characters
cellstrTo change strings from character array
validatestringTo Check validity of text string
symvarTo Determine symbolic variables in expression
regexpTo Match the regular expression
regexpiTo match the regular expression
regexprepTo Replace the given string
regexptranslateTo translate the string to reg expression
strncmpiTo Compare two characters of strings
upperTo Convert into uppercase
strjustTo Justify the character array

Examples

To illustrate few string functions mentioned above -

Formatting the Strings

We are creating a script file and type the following code-

N = pi*100*ones(1,8);

sprintf(' %f \n %.2f \n %+.2f \n %12.2f \n %012.2f \n', N)

we are kind of multiplying the pi value with 100 and creating a matrix od ones then try to print the N variable by formatting the strings as follows.

Output:

ans =
    ' 314.159265 
      314.16 
      +314.16 
            314.16 
      000000314.16 
      314.159265 
      314.16 
      +314.16 

let’s now execute this piece of code on MATLAB online to obtain the respective result as shown in the below screenshot.

Matlab Strings

Joining the Strings

We are Creating a script file and type the following code-

We are creating cell array of strings and then joining two strings by using symbols “^” and “$” and storing the value in variables st1 and st2 as shown in the code below:

%cell array of strings
strmyarray = {'r','b','g', 'y', 'o'};
% cell array into single string
st1 = strjoin(str_array, "^")
st2 = strjoin(str_array, "$")

Output:

st1 =
    'r^b^g^y^o'
st2 =
    'r$b$g$y$o'

let’s now execute this piece of code on MATLAB online to obtain the respective result as shown in the below screenshot.

Matlab Strings

Replacing the Strings

We are Creating a script file and type the following code-

We are creating a dictionary type object studentsNAME and then applying methods like strrep to replace the content of the created object. Next we are displaying the first student names.

studentsNAME = {'Zali', 'ieha Bhatt', ...
            'Mon Maik', 'padhu tam', ...
            'Madhu Sharma', 'phawna Sharma',...
            'yuu Ali', 'teva Dutta', ...
            'Sun Ali', 'pofia bir'};
 
new_studentname = strrep(studentsNAME(8), 'Reva', 'Poulomi')
% Displaying the first student names
first_names_OF_STUDENT = strtok(studentsNAME)

OUTPUT:

new_studentname =
  1×1 cell array
    {'teva Dutta'}
first_names_OF_STUDENT =
  1×10 cell array
    {'Zali'}    {'ieha'}    {'Mon'}    {'padhu'}    {'Madhu'}    {'phawna'}    {'yuu'}    {'teva'}    {'Sun'}    {'pofia'}

let’s now execute this piece of code on MATLAB online to obtain the respective result as shown in the below screenshot.

Matlab Strings

Multidimensional matrix as input

In this instance let us walk through an input which is a multidimensional matrix 3x3 dimensions (three rows and three columns). input is taken as a cell-matrix in addition to input data in alphabets format like { ' j ' , ' t ' , ' s ' ; ' a ' , ' a ' , ' p ' ; ' b ' , ' q ' , ' d ' }  as shown below code.

Code:

clc ;
clear all ;
myinput = { ' j ' , ' t ' , ' s ' ; ' a ' , ' a ' , ' p ' ; ' b ' , ' q ' , ' d ' }
out1 = char ( myinput )

Output:

myinput =
  3×3 cell array
    {' j '}    {' t '}    {' s '}
    {' a '}    {' a '}    {' p '}
    {' b '}    {' q '}    {' d '}
out1 =
  9×3 char array
    ' j '
    ' a '
    ' b '
    ' t '
    ' a '
    ' q '
    ' s '
    ' p '
    ' d '
 myinput = { ' j ' , ' t ' , ' s ' ; ' a ' , ' a ' , ' p ' ; ' b ' , ' q ' , ' d ' }

let’s now execute this piece of code on MATLAB online to obtain the respective result as shown in the below screenshot.

Matlab Strings

Declaring and initializing data types into one object

Now let us take input in form of various types like as integer, character, string, and float. In our code, input is assigned with a one-dimensional vector, data as follows ‘world  HELLO ‘, ‘ 756 ‘, ‘ BYE ‘, ‘ 22.98 ‘,’ H E Y  ‘, ‘ 190 ‘ }. in this code world HELLO, BYE, HEY are strings. 365 and 190 are integers and 22.98 is different datatype i.e float.

Example:

clc ;
clear all ;
myinput = {'world HELLO ' , ' 756 ' , ' BYE ' , ' 22.98 ' ,' H E Y ' , ' 190 ' }
myout = char( myinput )
myout ( 3 )
myout(5)
myout(1)

Output:

myinput =
  1×6 cell array
    {'world HELLO '}    {' 756 '}    {' BYE '}    {' 22.98 '}    {' H E Y '}    {' 190 '}
myout =
  6×12 char array
    'world HELLO '
    ' 756        '
    ' BYE        '
    ' 22.98      '
    ' H E Y      '
    ' 190        '
ans =
    ' '
ans =
    ' '
ans =
    'w'

Conclusion

In this tutorial, we have learnt how to change cell array into data type string by utilizing char function or also string function. We can transform all, character, types of data integer, types of float, etc. into the prescribed string format. We can transform formats like float, one-dimensional array, and single element and so on into the required format of data.