Characters and Strings in MATLAB
Last Updated :
15 Mar, 2022
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:
Matlab
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
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:
- Indexing: The index is used to select the subset of the text from the sequence of characters. To perform the index operation in Matlab the index should be enclosed within parenthesis().
Example 3:
Matlab
practice = 'Geeksforgeeks' ;
practice(1:5)
|
Output:
ans='Geeks'
Example 4:
Output:
ans='G'
- Concatenate: Concatenate character vector with square brackets, merges two different characters in one.
Example 5:
Matlab
str1 = 'Geeks' ;
str2 = 'Premier League' ;
str3 = ' 2022' ;
contest = [str1,str2,str3]
|
Output:
contest = 'GeeksPremier League 2022'
- append: append function inserts a new character to the existing character. The functionality of concatenating is the same but the append function is recommended because it treats string arrays, character vectors, and cell arrays of character vectors consistently.
Example 6:
Matlab
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
competition = "Code India Code"
whose competition
|
Output:

String Operations:
- strlength: Lengths of strings return the total number of characters in a given string.
Example 8:
Matlab
str = "Geeks Premier League" ;
num = strlength(str)
|
Output:
num=20
- isstring: Checks if the given string is string or not. Returns 0 if it is not a string and 1 if is a string.
Example 9:
Matlab
str = isstring( 'Mathworks' )
|
Output:
str = 0
0 because it is false, the entered value is in Character(enclosed within single quotes).
Example 10:
Matlab
str = isstring( "Mathworks" )
|
Output:
1
- replace: When you have a string and want to update the old content to new then replace function is used. Using replace function you can modify the old string data to the new data within the same variable.
Example 11:
Matlab
change = "Coding is tough" ;
str = replace(change, "tough" , "easy" );
display( "Before:" +change)
display( "After:" +str)
|
Output:
"Before: Coding is tough"
"After:Coding is easy"
- lower and upper: lower and upper functions are used to modify the given string. lower convert strings to lowercase whereas upper convert strings to uppercase.
Matlab
lower GEEKSFORGEEKS
upper geeksforgeeks
|
Output:

- reverse: The reverse function is used to return the same string but in reverse order.
Example 12:
Matlab
str1 = reverse( "Matlab is fun" )
|
Output:
str= "nuf si baltaM"
- strcmp: When we have two different strings Matlab even allows us to compare these two strings. Using strcmp, you can compare strings. If it is equal it will return 1 and if is not equal it will return 0.
Note: Comparing is case-sensitive.
Example 13:
Matlab
str1 = 'Geeksfor' ;
str2 = 'forGeeks' ;
cmp = strcmp(str1,str2)
|
Output:
0
Example 14:
Matlab
str1 = 'Geeks' ;
str2 = 'Geeks' ;
cmp = strcmp(str1,str2)
|
Output:
1
Similar Reads
Scripts and Functions in MATLAB
In MATLAB there are a different kinds of files dedicated to MATLAB codes. They are the following: ScriptLive ScriptFunction only fileClass fileNow only the live script is the only one of these which has a different extension name; all other three use the standard .m extension. In this article, we sh
2 min read
Iterating Over Characters of a String in R
In R Language a string is essentially a sequence of characters. Iterating over each character in a string can be useful in various scenarios, such as analyzing text, modifying individual characters, or applying custom functions to each character. This article covers different methods to iterate over
3 min read
MATLAB Find Exact String in Cell Array
Cell arrays in MATLAB store data of various data types as a cell. These cells could contain data of different types but belong to the same array. Now, this article is focused on finding an exact string in a cell array in MATLAB. This can be done easily by using a combination of two MATLAB functions,
2 min read
Add Functions to Scripts in MATLAB
From MATLAB version 2016b, it is possible to add functions directly into a script or live script. In this article, we shall how to add functions to script files. Â The syntax is simple except one rule that the function body must be written after the codes in the script. statement 1 statement 2 . stat
2 min read
Characters in Julia
Julia is a dynamic, high-level programming language with high performance and speed that is used to perform operations in scientific computing. It is great for computational complex problems. It is an open-source language so all source code is available easily online. It is as easy to use as Python
2 min read
How to reverse a string in MATLAB?
In this article, we are going to discuss the "Reversing of a string" in MATLAB which can be done in multiple approaches that are illustrated below: Method 1: Using Built-in Functions By using MATLAB built-in functions like reverse(), flip() and fliplr(). Built-in functions can be defined as - " The
3 min read
Creating Apps Using App Designer in MATLAB
MATLAB is a powerful, high-level programming language. Matlab is widely used for designing systems by engineers and scientists and we all know that the best way to represent any idea is by using a simple but effective GUI. Matlab app builder provides you the power to build different apps, to represe
3 min read
How to Extract Characters from a String in R
Strings are one of R's most commonly used data types, and manipulating them is essential in many data analysis and cleaning tasks. Extracting specific characters or substrings from a string is a crucial operation. In this article, weâll explore different methods to extract characters from a string i
4 min read
How To Create Video From An Image Using MATLAB?
MATLAB is a high-performance language that is used for manipulating matrices, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. With the help of this software, we can create videos from images. STEPS:Step 1. Bring the images to the folder where we are writing
2 min read
Write Data to Text Files in MATLAB
Writing data to a text file means creating a file with data that will be saved on a computer's secondary memory such as a hard disk, CD-ROM, network drive, etc. fprintf() function is used to write data to a text file in MATLAB. It writes formatted text to a file exactly as specified. The different e
3 min read