0% found this document useful (0 votes)
26 views15 pages

Lecture 5

Uploaded by

Souvik Saha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views15 pages

Lecture 5

Uploaded by

Souvik Saha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

IT Workshop

Programming in MATLAB
Lecture-5: Strings and String Arrays
Characters and Strings

• Character arrays and string arrays provide storage for text


data in MATLAB.
• A character array is a sequence of characters, just as a
numeric array is a sequence of numbers.
• A typical use is to store short pieces of text as character
vectors, such as c = 'Hello World'.

• A string array is a container for pieces of text. String


arrays provide a set of functions for working with text
as data.
Represent Text with String Arrays

• You can store any 1-by-n sequence of characters as a string,


using the string data type. Enclosing text in double quotes
creates a string.

str =
str = "Hello, world"
"Hello, world"

• To find the number of characters in a string, use


the strlength function.
n = strlength(str)
n= 12
Access Elements of String Array

o String arrays support array operations such as indexing and reshaping.


o Use array indexing to access the first row of str and all the columns.

str = ["Mercury","Gemini","Apollo"; "Skylab","Skylab B","ISS"];


str(1,:)

ans = 1x3 string


"Mercury" "Gemini" "Apollo"

Access the second element in the second row of str.

str(2,2)
ans =
"Skylab B"
Split String and Find Unique Words

• To find the unique words in a string, split it on space characters and call the unique
function.

• First, create a string scalar.


str = "A horse! A horse! My kingdom for a horse!“
str =
"A horse! A horse! My kingdom for a horse!"

• Remove the exclamation point.


str = erase(str,"!")
str =
"A horse A horse My kingdom for a horse"
• Split str on space characters using the split function.
split discards the space characters and returns the
result as a string array.
• Convert all letters in str to lowercase characters.
str = split(str)
str = lower(str)
str = str = 9x1 string
"a horse a horse my kingdom for a horse" "a"
"horse“
"a"
"horse"
"my"
"kingdom"
"for"
"a"
"horse"
Assign a new string outside the bounds of
str..
MATLAB expands the array and fills unallocated
elements with missing values
For Example
str = ["Mercury","Gemini","Apollo";"Skylab","Skylab B","ISS"];
str(3,4) = "Mir" str
2×3 string array

str = 3x4 string "Mercury" "Gemini" "Apollo"


"Mercury" "Gemini" "Apollo" <missing> "Skylab" "Skylab B" "ISS"
"Skylab" "Skylab B" "ISS" <missing>
<missing> <missing> <missing> "Mir"
Access Characters Within Strings

You can index into a string array using curly braces, {}, to access characters
directly.
Use curly braces when you need to access and modify characters within a string
element.

Access the second element in the second row with curly


braces.

str =
["Mercury","Gemini","Apollo";
"Skylab","Skylab B","ISS"];
chr = str{2,2}

chr =
'Skylab B'
Access the character vector and return the
first three characters.
str =
["Mercury","Gemini","Apollo";
"Skylab","Skylab B","ISS"];
chr = str{2,2}

chr =
'Skylab B'

str{2,2}(1:3)
ans =
'Sky
Find the space characters in a string and
replace them with dashes.
Use the isspace function to inspect individual characters within the string.
isspace returns a logical vector that contains a true value wherever there
is a space character

TF = isspace(str{2,2}) For Example


TF = 1x8 logical array str =
["Mercury","Gemini","Apollo";
0 0 0 0 0 0 1 0
"Skylab","Skylab B","ISS"];
Replace spaces using the replace function chr = str{2,2}
chr =
replace(str(2,2)," ","-")
ans = 'Skylab B'
"Skylab-B"
Concatenate Strings into String Array

Concatenate two string arrays using square brackets, [].

str1 = ["Mercury","Gemini","Apollo"];
str2 = ["Skylab","Skylab B","ISS"];
str = [str1 str2]
str = 1x6 string
"Mercury" "Gemini" "Apollo" "Skylab" "Skylab B" "ISS"
Concatenate strings horizontally

Syntax
Concatenate Two Cell Arrays:-
s = strcat(s1,...,sN)
firstnames = {'Abraham’; 'George'};
Examples:-
lastnames = {'Lincoln'; 'Washington'};
s1 = 'Good’;
names = strcat(lastnames, {', '},
s2 = 'morning’; firstnames)
s = strcat(s1,s2) names = 2x1 cell
s= {'Lincoln, Abraham' }
'Goodmorning' {'Washington, George'}
Concatenate arrays vertically

• Syntax
C = vertcat(A,B)
C = vertcat(A1,A2,…,An)
example:-
A1 = ["str1" "str2"];
A2 = ["str3" "str4"];
A3 = ["str5" "str6"];
C = vertcat(A1,A2,A3)
C = 3x2 string
"str1" "str2"
"str3" "str4"
"str5" "str6"
Analyze Text Data with String Arrays

Import Text File to String Array


Read text from Shakespeare's Sonnets with the fileread function. fileread returns the text
as a 1-by-100266 character vector.

s = fileread('Souvik.txt’)
ans =
‘Souvik Saha’
Assignment-3

Create two strings, where:


str1 contains Mercury, Gemini, Apollo
str2 contains Skylab, Skylab B, ISS
Transpose str1 and str2.
Concatenate them and then vertically
concatenate column headings (“Mission:”
and “Station:”)onto the string array.

You might also like