Handling text value and characters in MATLAB

By Soumya Srivastava and Shruti Datt on July 18, 2017

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.

Reference

  • Kanetkar, Y. P. (2016). Let Us C (14th ed.). BPB Publications.
Soumya Srivastava
Latest posts by Soumya Srivastava (see all)
 

Discuss