View all functions

CategoryStrings: Search

What does the strfind function do in MATLAB / RunMat?

k = strfind(str, pattern) returns the starting indices of every occurrence of pattern inside str. The builtin mirrors MATLAB semantics for character vectors, string scalars, string arrays, and cell arrays of character vectors.

How does the strfind function behave in MATLAB / RunMat?

  • Accepts text inputs as string scalars/arrays, character vectors/arrays, or cell arrays of character vectors. Mixed combinations are permitted.
  • Applies MATLAB-style implicit expansion when either argument is non-scalar.
  • Returns a numeric row vector when both inputs are scalar text (character vectors or string scalars) and 'ForceCellOutput' is false. If either input is a cell array or the broadcasted result contains multiple elements, the output is a cell array with the broadcasted shape, and each cell contains a row vector of double indices.
  • Pattern matches are case-sensitive and may overlap. For example, strfind("aaaa","aa") yields [1 2 3].
  • An empty pattern matches the boundaries between characters, producing indices 1:length(str)+1. Missing strings (<missing>) never match any pattern.
  • Specify 'ForceCellOutput', true to always obtain a cell array, even for scalar inputs.

strfind Function GPU Execution Behaviour

strfind performs substring searches on the host CPU. When its inputs currently live on the GPU (for example, after other accelerated operations), RunMat gathers them automatically before executing the search so the behaviour matches MATLAB exactly. No provider hooks are required.

Examples of using the strfind function in MATLAB / RunMat

Find substring positions in a character vector

idx = strfind('abracadabra', 'abra');

Expected output:

idx = [1 8];

Locate overlapping matches

idx = strfind("aaaa", "aa");

Expected output:

idx = [1 2 3];

Return matches for each element of a string array

words = ["hydrogen"; "helium"; "lithium"];
idx = strfind(words, "i");

Expected output:

idx = 3×1 cell array
    {[]}
    {[4]}
    {[2 5]}

Search with multiple patterns against one subject

idx = strfind("saturn", ["sat", "turn", "moon"]);

Expected output:

idx = 1×3 cell array
    {[1]}    {[3]}    {[]}

Force cell output for scalar inputs

idx = strfind('mission', 's', 'ForceCellOutput', true);

Expected output:

idx = 1×1 cell array
    {[3 4]}

Handle empty patterns and missing strings

idxEmpty = strfind("abc", "");
idxMissing = strfind("<missing>", "abc");

Expected output:

idxEmpty = [1 2 3 4];
idxMissing = [];

GPU residency in RunMat (Do I need gpuArray?)

You usually do NOT need to call gpuArray yourself in RunMat (unlike MATLAB).

strfind always executes on the host, but if you pass in GPU-resident values RunMat gathers them automatically before performing the search. This keeps the results identical to MATLAB while still allowing upstream computations to benefit from acceleration.

FAQ

What types can I pass to strfind?

Use string scalars/arrays, character vectors/arrays, or cell arrays of character vectors for either argument. Mixed combinations are allowed; MATLAB-style implicit expansion aligns the inputs automatically.

How are the results formatted?

When both inputs are scalar text (character vectors or string scalars) and 'ForceCellOutput' is false, strfind returns a numeric row vector with the starting indices. In all other cases — for example, if either input is a cell array or the broadcasted size exceeds one element — the result is a cell array whose shape matches the broadcasted size, with each cell containing a row vector of doubles.

Do matches overlap?

Yes. The builtin considers every occurrence of the pattern, including overlapping matches.

What happens with empty patterns?

An empty pattern matches the gaps between characters, so the indices 1:length(str)+1 are returned for non-missing text. When the subject is missing, the result is an empty array.

How do I always get a cell array?

Supply 'ForceCellOutput', true. This mirrors MATLAB and is useful when you want consistent cell outputs regardless of input sizes.

Does strfind support case-insensitive matching?

No. strfind is case-sensitive like MATLAB. Use contains, startsWith, or the regular expression functions when you need case-insensitive searches.

See Also

contains, startsWith, endsWith, regexp

Source & Feedback