0% found this document useful (0 votes)
6 views20 pages

CST Characters and Strings

The document provides an overview of character and string handling in MATLAB, including how to create character arrays and string arrays, convert between them, and manipulate text data. It explains functions for concatenation, conversion, and finding unique words, as well as methods for replacing substrings and checking if an input is a string array. Additionally, it includes examples and code snippets to illustrate these concepts.

Uploaded by

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

CST Characters and Strings

The document provides an overview of character and string handling in MATLAB, including how to create character arrays and string arrays, convert between them, and manipulate text data. It explains functions for concatenation, conversion, and finding unique words, as well as methods for replacing substrings and checking if an input is a string array. Additionally, it includes examples and code snippets to illustrate these concepts.

Uploaded by

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

Computational Scientific Tool

Characters and Strings


Character Vector
• Character arrays and string arrays provide storage for text data in
MATLAB.
• A character array is a sequence of characters. A string array is a
container for pieces of text.
A = ‘MATLAB’
C=A(1)
Output C = M
Character Vector
• Concatenate character arrays with square brackets,
seq = 'GCTAGAATCC';
seq2 = [seq 'ATTAGAAACC']
seq2 = 'GCTAGAATCCATTAGAAACC'
Convert Numeric Array to Character array

A= [77 65 84 76 65 66];
C=char(A)
C=‘MATLAB’

A2 array has 4 columns.


char pads row of from A2 with
blank. A1 = [65 66; 67 68];
A1= [77 65 84 76 65 66]; A2 = ‘abcd’;
A2= [55 54 65 66]; C=char(A1,A2)
C= char(A1,A2) C = 3x4 char array
C = 2x6 char array ‘AB ’
‘MATLAB’ ‘CD ’
’76AB ’ ‘abcd’
Convert string to character array
• We can create string scalars using double quotes.
A = “Pythagoras”
A = “Pythagoras”

• Convert A to a character vector using the char function.


MATLAB displays character vectors with single quotes.
C= char(A)
C=‘Pythagoras’
Convert Datetime to character vector
• d= ‘2018-06-25 11:23:37.712’;
t=datetime(d, ‘InputFormat’, ‘yyyy-MM-dd HH:mm:ss.SSS’)
c= char(t)
String
• str = "Hello, world"

n= strlength(str)
String
• If the text includes double quotes, use two double quotes
within the definition.
q = "Something ""quoted"" and something else.“

• You also can convert variables of different data types into


string arrays using the string function
• str = string(A) converts the input array to a string array.
String
• Convert character array to string
A = 'Four score and seven years ago‘
str = string(A)
• Convert numeric array to string
A = [77 65 84 76 65 66]
str = string(A)
String
• To add text to the end of a string, use the plus operator, +. If a
variable can be converted to a string, then plus converts it and
appends it.
• f = 71;
c = (f-32)/1.8;
tempText = "Temperature is " + c + "C“;
tempText2 = append(“Today’s ”, tempText)
String
• To convert the string array to a numeric array, use
the double function.
• str = ["256","3.1416","8.9e-3"]
X = double(str)
or
Y = str2double(str)
• str2double is suitable when the input argument might be a
string array, character vector, or cell array of character
vectors.
String
• To find the unique words in a string
str = "A horse! A horse! My kingdom for a horse!“;
str = erase(str,"!");
str = lower(str)
• Split str on space characters using the split function.
split discards the space characters and returns the result as a
string array.
str = split(str);
str = unique(str)
str = 5x1 string "a" "for" "horse" "kingdom" "my"
strfind function
• k = strfind(str,pattern)
• Example
• str = 'Find the starting indices of a pattern in a character
vector';
k = strfind(str,'in')
• Output - k = 1×4
2 15 19 40

Link for other functions


https://in.mathworks.com/help/matlab/characters-and-
strings.html?s_tid=CRUX_lftnav
Find and Replace

newStr = replace(str,old,new) replaces all occurrences of the


substring old with new.
Question
• Create a character vector with a repeated, overlapping
pattern. Compare the results of using the strrep and replace.
Join and Split
Edit
Compare
String Array
• Similar to numeric arrays, string arrays can have multiple
elements. Use the strlength function to find the length of
each string within an array.
A = ["a","bb","ccc"; "dddd","eeeeee","fffffff"]
strlength(A)
ans = 1 2 3
467
String Array
• Determine if input is string array
• tf = isstring(A)
• tf = isstring(A) returns 1 (true) if A is a string array. Otherwise,
it returns 0 (false).
chr = 'Mary Jones‘
tf = isstring(chr)
• tf=0 because character vector is not string.
• str = ["Smith","Chung","Morales";
"Sanchez","Peterson","Adams"]
tf = isstring(str)
output is 1.

You might also like