MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
TF = contains(chr,'Paul')

TF = logical
1

TF = endsWith(chr,'Paul')

TF = logical
0

Use the contains function to find text in rows of a string array. census1905 contains a
few rows of simulated census data for the year 1905. Each row contains a name, year of
birth, and number of times that name was given in that year.

census1905 = ["Ann Mary","1905","230";
"John","1905","5400";
"Mary","1905","4600";
"Maryjane","1905","304";
"Paul","1905","1206"];

Find the rows where the name is equal to Mary.

TF = (census1905(:,1) == "Mary");
census1905(TF,:)

ans = 1x3 string array
"Mary" "1905" "4600"

Find the rows where the name is a variation of Mary with the contains function.

TF = contains(census1905(:,1),"Mary");
census1905(TF,:)

ans = 3x3 string array
"Ann Mary" "1905" "230"
"Mary" "1905" "4600"
"Maryjane" "1905" "304"

Replace Text

You can replace text in string arrays, character vectors, or cell arrays of character vectors
with the replace function.

6 Characters and Strings

Free download pdf