# Find string matlab ## strfind

### Description

example

searches for occurrences of . The output, , indicates the starting index of each occurrence of in . If is not found, then returns an empty array, . The function executes a case-sensitive search.

• If is a character vector or a string scalar, then returns a vector of type .

• If is a cell array of character vectors or a string array, then returns a cell array of vectors of type .

example

forces to return as a cell array when is true, even when is a character vector.

collapse all

### Find Substrings in Character Vector

Find the starting indices of substrings in a character vector.

First, create a character vector.

str = 'Find the starting indices of substrings in a character vector';

Find the substring .

There are five instances in .

Find the substring .

Since is case sensitive, the substring is not found. is an empty array.

Find the blank spaces in .

k = 1×9 5 9 18 26 29 40 43 45 55

There are ten blank spaces in .

### Find Letters and Words Using Patterns

Since R2020b

Create a character vector.

str = 'Find the letters.'
str = 'Find the letters.'

Create a pattern that matches sequences of letters using the function.

pat = pattern Matching: lettersPattern

Find the index of each letter. While matches a sequence of letters having any length, stops as soon as it finds a match and then proceeds to the next match. For example, and are both matches for , since the number of letters for a match is not specified. But matches first and returns its index. Then matches , and so on. (You can call with an optional argument that specifies the number of letters to match.)

k = 1×14 1 2 3 4 6 7 8 10 11 12 13 14 15 16

To find the starts of words, call with boundaries. The function matches a boundary between letters and nonletter characters.

pat = letterBoundary + lettersPattern
pat = pattern Matching: letterBoundary + lettersPattern

For a list of functions that create pattern objects, see .

### Find Substrings in Cell Array

Find the starting indices of substrings in a cell array of character vectors.

Create a cell array of character vectors.

str = {'How much wood would a woodchuck chuck'; 'if a woodchuck could chuck wood?'};

Find in .

idx = strfind(str,'wood')
idx=2×1 cell array {[10 23]} {[ 6 28]}

Examine the output cell array to find the instances of .

The substring occurs at indices 10 and 23 in the first character vector and at indices 6 and 28 in the second character vector.

### Return Indices in Cell Array

Find the occurrences of a substring in a character vector. Force to return the indices of those occurrences in a cell array. Then display the indices.

Create a character vector and find the occurrences of the pattern .

str = 'The rain in Spain.'; k = strfind(str,'ain','ForceCellOutput',true)
k = 1x1 cell array {[6 15]}

returns a scalar cell that contains a numeric array, which contains indices of occurrences of the substring in . To access the numeric array within the cell, use curly braces.

collapse all

### — Input textstring array | character vector | cell array of character vectors

Input text, specified as a string array, character vector, or cell array of character vectors.

### — Search patternstring scalar | character vector | scalar (since R2020b)

Search pattern, specified as one of the following:

• String scalar

• Character vector

• scalar (since R2020b)

### — Indicator for forcing output to be returned as cell array (default) | | |

Indicator for forcing output to be returned as a cell array, specified as , , , or .

collapse all

### — Indices of occurrences of array

Indices of occurrences of , returned as an array. If is not found, then is an empty array, .

• If is a character vector or a string scalar, is a vector of doubles indicating the index of each occurrence of .

• If is a cell array of character vectors or a string array, is a cell array. For each piece of text in , the corresponding cell of contains a vector of doubles indicating the index of each occurrence of .

### Tips

• If is a character vector or string scalar with no characters ( or ), then returns an empty array.

• Starting in R2016b, the function is recommended for finding patterns within string arrays.

### Tall ArraysCalculate with arrays that have more rows than fit in memory.

Usage notes and limitations:

• The text input must be a tall array of strings or a tall cell array of character vectors.

• The pattern input must be a single string, and it must not be a tall array or a pattern object.

• The output is a tall cell array of index vectors, with one element per input string.

### C/C++ Code GenerationGenerate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

• must be a string scalar or a character vector.

• Generated code returns an empty output as a 1-by-0 character array.

### Distributed ArraysPartition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

Usage notes and limitations:

•  must be a string array or a cell array of character vectors.

For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Introduced before R2006a

You have a modified version of this example. Do you want to open this example with your edits?

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.

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: .

Select web site

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.

Sours: https://www.mathworks.com/help/matlab/ref/strfind.html

## MATLAB Find String

This tutorial will discuss how to find strings within other strings using the function in MATLAB.

### Find Strings Within Other Strings Using the Function in MATLAB

To find occurrences of a string within another string, we can use the function in MATLAB. The first argument of the function is the string from which you want to find the occurrences of a substring, and the second argument is the string or character you want to find. The output of this function is a vector containing the indices or position of the occurrences of the substring. We can also find the occurrences of a string within an array of strings, and the output will be an array of vectors of indices of the occurrences of the substring. For example, let’s find the occurrences of the space character and the number of spaces within a string using the function. See the code below.

Output:

In the above code, we used the function to find the total number of spaces present in the string. In the output, the indices show the position of the space character present in the string. You can change the string and the character you want to find according to your requirements, and if you want to find a specific character or string from an array of strings, you can also find it using this function. Check this link for more details about the function.

Contribute

DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

### Related Article - MATLAB String

• Convert ASCII to String in MATLAB
• Sours: https://www.delftstack.com/howto/matlab/matlab-find-string/

## MATLAB: Find index of cells containing the string

Do you want to search for 'bla' within the text in each element of the cell array, or for elements that are 'bla' exactly? If you explain this detail, then your question would be easier to answer.
If you are searching for text that has 'bla' as part of the text, then starting in R2016b you can use the “contains” function, as Alexander Cranney pointed out.
The function "contains" returns a logical array. This type of logical indexing can be used to make many workflows more efficient. For more on using logical arrays, refer to the documentation:
In previous versions of MATLAB (before R2016b), you can use the “strfind” function. However, “strfind” returns a cell array of indices. For any input cell whose text does not contain 'bla', “strfind” returns an empty cell. Use “isempty” and “cellfun” with the “find” function to find the empty cells.
If you are searching for text that is exactly 'bla', then see Jos’ answer.
Sours: https://itectec.com/matlab/matlab-find-index-of-cells-containing-the-string/
MATLAB Video 4: Basic Strings, Concatenating Strings, and Char Function
 MATLAB Function Reference  strfind

Find one string within another

Syntax

• k = strfind(str, pattern) k = strfind(cellstr, pattern)

Description

searches the string for occurrences of a shorter string, , and returns the starting index of each such occurrence in the double array . If is not found in , or if is longer than , then returns the empty array .

searches each string in cell array of strings for occurrences of a shorter string, , and returns the starting index of each such occurrence in cell array . If is not found in a string or if is longer then all strings in the cell array, then returns the empty array , for that string in the cell array.

The search performed by is case sensitive. Any leading and trailing blanks in or in the strings being searched are explicitly included in the comparison.

Examples

Use to find a two-letter pattern in string :

• S = 'Find the starting indices of the pattern string'; strfind(S, 'in') ans = 2 15 19 45 strfind(S, 'In') ans = [] strfind(S, ' ') ans = 5 9 18 26 29 33 41

Use on a cell array of strings:

• cstr = {'How much wood would a woodchuck chuck'; 'if a woodchuck could chuck wood?'}; idx = strfind(cstr, 'wood'); idx{:,:} ans = 10 23 ans = 6 28

This means that occurs at indices 10 and 23 in the first string and at indices 6 and 28 in the second.

, , , , , , , , i, streamtube strings Sours: http://matlab.izmiran.ru/help/techdoc/ref/strfind.html

## Find index of cells containing my string

Do you want to search for 'bla' within the text in each element of the cell array, or for elements that are 'bla' exactly? If you explain this detail, then your question would be easier to answer.

If you are searching for text that has 'bla' as part of the text, then starting in R2016b you can use the “contains” function, as Alexander Cranney pointed out.

Index = find(contains(C,'bla'));

The function "contains" returns a logical array. This type of logical indexing can be used to make many workflows more efficient. For more on using logical arrays, refer to the documentation:

https://www.mathworks.com/help/matlab/math/array-indexing.html#MatrixIndexingExample-3

https://www.mathworks.com/help/matlab/matlab_prog/find-array-elements-that-meet-a-condition.html

In previous versions of MATLAB (before R2016b), you can use the “strfind” function. However, “strfind” returns a cell array of indices. For any input cell whose text does not contain 'bla', “strfind” returns an empty cell. Use “isempty” and “cellfun” with the “find” function to find the empty cells.

IndexC = strfind(C,'bla');

Index = find(not(cellfun('isempty',IndexC)))

If you are searching for text that is exactly 'bla', then see Jos’ answer.

MATLAB Video 4: Basic Strings, Concatenating Strings, and Char Function
 MATLAB Function Reference  strmatch

Find possible matches for a string

Syntax

• x = strmatch('str',STRS) x = strmatch('str',STRS,'exact')

Description

looks through the rows of the character array or cell array of strings to find strings that begin with string , returning the matching row indices. is fastest when is a character array.

returns only the indices of the strings in matching exactly.

Examples

The statement

• x = strmatch('max',strvcat('max','minimax','maximum'))

returns since rows 1 and 3 begin with . The statement

• x = strmatch('max',strvcat('max','minimax','maximum'),'exact')

returns , since only row 1 matches exactly.

, , , , , , , , i, strjust strncmp Sours: http://www.ece.northwestern.edu/local-apps/matlabhelp/techdoc/ref/strmatch.html

## Find string within cell array

I wanted to do this recently and was surprised none of the built-in string-based functions worked for this so I delved into regexp for the first time...

regexp( string, elements );

will give you the indices into your string to the start of any matching elements. By default the result would be a cell array of length equal to the length of your elements cell array and in each cell of the result will be an array of indices to the start of any matches of that element.

e.g.

string = 'This is a string not a cell, yes really a string, not a cell'; elements = {'string', 'cell'};

res = regexp( string, elements )

res =

[1x2 double] [1x2 double]

>> res{1}

ans =

11 43

>> res{2}

ans =

24 57

If you want you can get other outputs from regexp instead which are detailed in the help at

doc regexp

306 307 308 309 310