SoFunction
Updated on 2025-04-07

Example code of usage of count function in MATLAB

The function of the count function is to calculate the number of occurrences of patterns in a string.

grammar

A = count(str,pat)
A = count(str,pat,'IgnoreCase',true)

illustrate

A = count(str,pat) Returns the number of occurrences of pat in str.

If pat is an array containing multiple patterns, count returns the total number of times all elements of pat appear in str. count matches the elements of pat from left to right. The text segments in str can only match once.

A = count(str,pat,'IgnoreCase',true) When counting the number of occurrences of pat, upper and lower case will be ignored.

Example

Count the occurrence count

Count the number of occurrences of string red in the string array. Strings can be created using double quotes.

str = "paired with red shoes"
str = 
"paired with red shoes"

To calculate the number of 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

Creates an array of 2×1 strings.

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"

Calculates the number of occurrences of red in each element of str. If str is a string array or a cell array of character vectors, A is a numeric array of values ​​of the same size.

A = count(str,"red")
A = 2×1

     3
     1

Count numbers and letters using patterns

Create an array of strings containing 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 number of digits in each address, first create a pattern that matches a single number. This pattern appears in a string as the number of digits in the string.

Create this pattern by calling the digitsPattern function with 1 as the input parameter. When doing this, it matches a single number (such as 2), rather than an arbitrary sequence of numbers (such as 221 or 4059).

pat = digitsPattern(1)
pat = pattern
  Matching:

    digitsPattern(1)

Then call the count function and use str and pat as input.

A = count(str,pat)
A = 1×3

     3     0     4

Similarly, you can count the number of letters (excluding numbers, spaces, or punctuation) using the pattern created by lettersPattern(1).

A = count(str,lettersPattern(1))
A = 1×3

     8    21     7

Count a sequence of one or more numbers followed by a 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

All occurrences of multiple substrings

Calculates the total number of times red and blue appear in a string array. Strings can be created 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 appear once each. count returns 3 for the second string because red appears once and blue appears twice.

A = count(str,["red","blue"])
A = 2×1

     2
     3

Ignore case

Calculate lettersEThe number of occurrences in the string array containing the name, ignoring case. Strings can be created 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 vectors

Calculate the number of occurrences of al in the word alphabetical.

chr = 'alphabetical'
chr = 
'alphabetical'
A = count(chr,'al')
A = 2

Parameter description

str — Enter text

Enter text, specified as a string array, character vector, or character vector cell array.

pat — Search mode

Search mode, specified as one of the following values:

  • String array

  • Character vector

  • Character vector cell array

  • ​pattern array (provided since R2020b)

Summarize

This is all about this article about the usage of count function in MATLAB. For more related content on the usage of count function in MATLAB, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!