count
Count occurrences of pattern in strings
Description
Examples
Count Number of Occurrences
Count the number of occurrences of the string, red
, in string arrays.
You can create a string using double quotes.
str = "paired with red shoes"
str = "paired with red shoes"
To count the occurrences of red
, use the count
function. In this example, the result is 2 because red
is also part of the word paired
.
A = count(str,"red")
A = 2
Create a 2-by-1 string array.
str = ["red green red red blue blue green"; "green red blue green green blue"]
str = 2x1 string
"red green red red blue blue green"
"green red blue green green blue"
Count the occurrences of red
in each element of str
. If str
is a string array or cell array of character vectors, then A
is a numeric array that has the same size.
A = count(str,"red")
A = 2×1
3
1
Count Digits and Letters Using Patterns
Since R2020b
Create a string array that contains addresses.
str = ["221B Baker St.","Tour Eiffel Champ de Mars","4059 Mt Lee Dr."]
str = 1x3 string
"221B Baker St." "Tour Eiffel Champ de Mars" "4059 Mt Lee Dr."
To count the digits in each address, first create a pattern that matches a single digit. The number of times this pattern occurs in a string equals the number of digits in the string.
Create the pattern by calling the digitsPattern
function with 1
as the input argument. When you do this, it matches a single digit (such as 2
) instead of an arbitrary sequence of digits (such as 221
or 4059
).
pat = digitsPattern(1)
pat = pattern
Matching:
digitsPattern(1)
Then call the count
function with str
and pat
as inputs.
A = count(str,pat)
A = 1×3
3 0 4
Similarly, you can count the number of letters (not including digits, spaces, or punctuations marks) by using the pattern created by lettersPattern(1)
.
A = count(str,lettersPattern(1))
A = 1×3
8 21 7
Count sequences consisting of one or more digits and then one letter. You can build more complex patterns by combining simple patterns. In this case, digitsPattern + lettersPattern(1)
matches 221B
.
pat = digitsPattern + lettersPattern(1); A = count(str,pat)
A = 1×3
1 0 0
For a list of functions that create pattern objects, see pattern
.
All Occurrences of Multiple Substrings
Count the total number of occurrences of red
and blue
in a string array.
You can create strings using double quotes.
str = ["red green blue"; "green red blue green blue"]
str = 2x1 string
"red green blue"
"green red blue green blue"
count
returns 2 for the first string because red
and blue
each occur once. count
returns 3 for the second string because red
occurs once and blue
occurs twice.
A = count(str,["red","blue"])
A = 2×1
2
3
Ignore Case
Count the number of occurrences of the letter E
in a string array that contains names, ignoring case.
You can create strings using double quotes.
str = ["Edgar Allan Poe";"Louisa May Alcott"]
str = 2x1 string
"Edgar Allan Poe"
"Louisa May Alcott"
A = count(str,'E','IgnoreCase',true)
A = 2×1
2
0
Count Substrings in Character Vector
Count the number of times al
occurs in the word alphabetical
.
chr = 'alphabetical'
chr = 'alphabetical'
A = count(chr,'al')
A = 2
Input Arguments
str
— Input text
string array | character vector | cell array of character vectors
Input text, specified as a string array, character vector, or cell array of character vectors.
pat
— Search pattern
string array | character vector | cell array of character vectors | pattern
array (since R2020b)
Search pattern, specified as one of the following:
String array
Character vector
Cell array of character vectors
pattern
array (since R2020b)
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
The
count
function fully supports tall arrays. For more information,
see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
str
andpattern
must be a string scalar, a character vector, or a cell array containing not more than one character vector.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2016b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)