Handling text value and characters in MATLAB

As discussed in the previous article, a text value can also be assigned to a variable. The string variable used for input is identified as text & character variable in MATLAB. In the example below, a string or a sequence of characters are enclosed in single quotes.

assigning string to the variable in matlab for text value and characters

Image1:String assigned to text value

If the text value itself contains single quote; eg. Sam’s in the image 2 below, then three single quotes are used within the statement. Two for text and one for the apostrophe (See Image 2 below).

displaying the single quote in matlab

Image 2: Display single quotes within a statement

MATLAB stores data in array and matrix format. In case of string, data is stored in the form of an array. Once the string variable is stored in an array it becomes string array. Two string arrays can be concatenated or joined together using square brackets. In the following example (Image 3), two strings that is string stored in “d” i.e. hello and “text” i.e. ‘This is Sam’s file’ are joined together.

string arrays in matlab for text and structures

Image3: Concatenation of string arrays

Converting numeric values to string values

A numeric value can be converted to string using commands like numeric to string. It can be done using the  “num2str (Argument)” or integer to string i.e. “int2str (Argument)”.

For example in Image 4, variable “x” that holds a numeric value that is 2 is converted to string and used in the statement “hello, Elle turned “2” yesterday”.

process of converting the numeric value to string in matlab

Image 4: Conversion of numeric value to string

Quick Tip: As Image 4 shows operation “x=u-v” is followed by a semicolon. This is not done to process the operation but stop the results from getting displayed. This is because many a times not all information is displayed in the output.

Calling functions

Functions are pre-defined modules of code that once created can be used again and again just by their names instead of writing the whole code. Thus whenever a code of program is required, its function is “called” Kanetkar, (2016). For example, if a variable is to be displayed, then instead of writing the whole code for displaying the code, its corresponding function “disp()” is called. A function can be called by using parenthesis for entering input arguments.

For example: consider “num2str()” function in Image 4 above, the value of x was called and returned as string in the result.

Multiple argument: In case of multiple arguments, comma is used to separate the calling functions (Image 5):

single function multiple arguments

Image 5: Multiple arguments in a function

Assigning function output: Output of a function can also be returned by assigning it to a variable (Image 6)

variable and the process of assigning function to it

Image 6: Assigning function output to a variable

Multiple output arguments: In the above example minimum is the name of the variable in which the value returned from function “min()” is stored. In case there are multiple output arguments, then square brackets are used to enclose them as per the syntax defined in MATLAB (Image 7)

arguments with multiple outputs

Image 7: Multiple output arguments

In the above example, value returned by the function “min(B)”is assigned to variable “cat” and the location or position of the outcome of “min(B)” that is number 22 in array B is returned.

In case, function is using string argument, it is to be projected (‘hey’) in single quotes (Image 8).

Using string as an argument in a function.

Image8: Using string as an argument in a function.


  • Kanetkar, Y. P. (2016). Let Us C (14th ed.). BPB Publications.
Soumya Srivastava

Soumya Srivastava

Research Analyst at Project Guru
Soumya has completed her MBA in HR from National Institute of Technology (NIT), Kurukshetra and is Bachelor in Information Technology. She loves numbers, their patterns and the infinity nature possessed by them. When it comes to computers, she has interest in memory allocation and virtual memory. In her spare time she likes to read sun signs and personality traits. She believes in hard work and considers smart work to be incomplete without hard work.
Soumya Srivastava

Latest posts by Soumya Srivastava (see all)

Related articles

  • Search and help in MATLAB MATLAB provides various ways in which a user can access help within the software for better understanding of the functions. Similarly, many search options are provided to search text, characters and functions in the software in order to fix errors and avoid bugs.
  • Introduction of basic MATLAB interface and quick access tools Matrix Laboratoryn (MATLAB) is a high-level language that is matrix based and includes an interactive environment for numerical computation, visualization,development and programming which was originally written in FROTRAN (Formual Translation). The present module is based on R 2017a […]
  • Adding variables to workspace window in MATLAB As discussed in the previous article, workspace is a space wherein the variables used and created in a study are displayed. The variables can also be imported from data files or other programs.
  • Formatting output and help in MATLAB In the previous articles, the discussion revolved around the use of command window and how variables are added to working window. In the present article, the focus will be on how to format output and further use MATLAB for documentation without errors.
  • Building special constructs of nested loop in for & while loops in ‘R’ Nested loop with for, are popular command as it implies that the number of iterations are fixed and are known before applying. It is well known that R is preferably used for manipulating large sets of data, which consists of matrix, data frames and lists.


We are looking for candidates who have completed their master's degree or Ph.D. Click here to know more about our vacancies.