MATLAB String

After reading the MATLAB String topic, you will understand how to create and manipulate the string, and you will understand how to use string built-in functions in MATLAB.

An array of characters is called string. It is created by typing the characters within single quotes. Characters that can be used in the string as lowercase letters (a – z), uppercase letters (A – Z), digits (0 – 9) and other characters as (#,

How to write the string in MATLAB?

A string writes inside a pair of the single quotation mark. The statement in MATLAB is given by

Output

x =
programming

Accessing Character from String

Consider the statements in MATLAB is given by

Output

ans =
p
ans =
r

Explanation

  • statement x(1) gives output p describe the first character of string (i.e. programming) is associated with index number 1.
  • statement x(2) gives output r describes the second character of string (i.e. programming) is associated with index number 2.

Accessing Multiple Character from String

Consider the statements in MATLAB is given by

Output

ans =
     prog

Explanation

  • statement x(1:4) gives output prog describe accessing characters of the string associated with index numbers 1 to 4.

String handling build-in Functions

Some of commonly used string handling functions like strjoin(), lower(), upper() and strcmp(), .

strjoin() : This function to join two or more strings.

Consider the statements in MATLAB is given by

C = {'MATLAB','PROGRAMMING'}
str = strjoin(C)

Output

str =
MATLAB PROGRAMMING

Explanation:

  • statement str = strjoin(C) join two strings as ‘MATLAB’ and ‘PROGRAMMING’ and stores in variable str
  • Hence the output is MATLAB PROGRAMMING.

strcmp(): This function compares two strings and gives 1(true) as an output when both strings are same, otherwise gives 0(false) as an output.

Consider the statements in MATLAB is given by

s1 = 'MATLAB';
s2 = 'HELP';
tf = strcmp(s1,s2)

Output

tf =
     0

Explanation

  • Strings ‘MATLAB’ and ‘HELP’ not same, so the output is 0.

lower() : This function converts the complete string into a lowercase string.

Consider the statements in MATLAB is given by

txt = 'MATLAB PROGRAMMING';
newTxt = lower(txt)

Output

newTxt =
matlab programming

Explanation

  • The variable txt stores the string ‘MATLAB PROGRAMMING’.
  • statement newTxt = lower(txt), display and convert string ‘MATLAB PROGRAMMING’ into lowercase string as matlab programming.

upper() : This function converts the complete string into an uppercase string.

Consider the statements in MATLAB is given by

txt = 'matlab programming';
newTxt = upper(txt)

Output

newTxt =
MATLAB PROGRAMMING

Explanation

  • The variable x stores the string ‘MATLAB’.
  • statement newTxt = upper(txt) print and convert string ‘matlab programming’ into uppercase string as MATLAB PROGRAMMING.

char Command

The char command creates rows of string. The general form of the command is:

variable_name = char('string 1','string 2','string 3')

The example below will show you how to use char command in MATLAB.

Example

Aim (1): To store students data as given below in MATLAB.

Software Name – MATLAB; Version – 3; Launch Year – 2018

Program (1):

x=char('Software Name - MATLAB','Version – 3','Launch Year - 2018')

Output (1):

x =
Software Name - MATLAB
Version – 3          
Launch Year – 2018

Program (2): To store string “Hello” in a variable also store the string “My name is MATLAB” in another variable.

a='Hello'
b='My name is MATLAB'

MATLAB VIEW – Program (1):

Create a script file and type the following code –

Output (2):

a =
Hello
b =
My name is MATLAB

MATLAB VIEW – Output (2):

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!
Scroll to Top