Open In App

Characters and Strings in MATLAB

In this article, we shall see how to deal with characters and Strings in MATLAB. A data type is an attribute/keyword that specifies the type of data that the object can hold: numeric data or text data. By default, MATLAB stores all numeric variables as double-precision floating-point values. Additional data types store text, integer or single-precision values, or a combination of related data in a single variable. 

The text data is stored in a character array and String array. We shall the use case of both character and string in MATLAB:



Character:

Just like in C/C++/Java, a character is a data type that stores single character data within single quotes. In Matlab, you store the entire text within single quotes and it will be treated as a character.

Example 1:






% Character is enclosed within single quotes
chr = 'Geeksforgeeks'
whose chr

 
 

Output:

 

The text ‘Geeksforgeeks’ is 13 characters long, and chr stores it as a 1-by-13 character vector. As in the above image you can notice that the chr variable belongs to the Char class i.e., character. 

 

If the text includes single quotes, use two single quotes within the definition.

 

Example 2:

 




% MATLAB code for Single Quote
% Sentence Inside Character
chr = 'Geeksforgeeks is hosting, '
'Geeks Premier League'' for all its writers.';

 
 

Output:

 

chr = 'Geeksforgeeks is hosting, ''Geeks Premier League'' for all its writers.'

Character Operation in MATLAB:

Example 3:

 




% MATLAB code for indexing
practice = 'Geeksforgeeks';
practice(1:5)

Output:

ans='Geeks'

Example 4: 




% MATLAB code for indexing
practice(1)

 
 

Output:

 

ans='G'

Example 5: 

 




% MATLAB code for Concatenate
str1 = 'Geeks';
str2 = 'Premier League';
str3 = ' 2022';
contest = [str1,str2,str3]

 
 

Output:

 

contest = 'GeeksPremier League 2022'

Example 6: 

 




% MATLAB code for append
com = 'Geeks';
contest = append(com,'Premier League');
display(contest)

Output:

contest = 'GeeksPremier League'

String

String arrays provide a set of functions for working with text as data, i.e., it is a sequence of characters that is enclosed within double-quotes. 

Example 7: 




% MATLAB code for string with whose
competition = "Code India Code"
whose competition

 
 

Output:

 

String Operations:

Example 8: 

 




% MATLAB code for get a string length
str = "Geeks Premier League";
num = strlength(str)

 
 

Output:

 

num=20

Example 9: 

 




% MATLAB code for isstring
str = isstring('Mathworks')

 
 

Output:

 

str = 0

0 because it is false, the entered value is in Character(enclosed within single quotes).

 

Example 10: 

 




% MATLAB code for isstring
str = isstring("Mathworks")

Output:

1

Example 11: 




change = "Coding is tough";
str = replace(change,"tough","easy");
display("Before:" +change)
display("After:" +str)

 
 

Output:

 

"Before: Coding is tough"
"After:Coding is easy"

 




% MATLAB Code for String Lower and Upper
lower GEEKSFORGEEKS
upper geeksforgeeks

 
 

Output:

 

Example 12: 

 




str1 = reverse("Matlab is fun")

 
 

Output:

 

str= "nuf si baltaM"

Note: Comparing is case-sensitive. 

 

Example 13: 

 




str1 = 'Geeksfor';
str2 = 'forGeeks';
cmp = strcmp(str1,str2)

 
 

Output:

 

0

Example 14: 

 




str1 = 'Geeks';
str2 = 'Geeks';
cmp = strcmp(str1,str2)

Output:

1

Article Tags :