endsWith — Test whether text inputs end with patterns in MATLAB and RunMat.
endsWith(str, pattern) returns logical results indicating whether each element of str ends with the corresponding pattern. Supported text types and implicit expansion behavior follow MATLAB semantics.
Syntax
tf = endsWith(str, pat)
tf = endsWith(str, pat, ignoreCase)
tf = endsWith(str, pat, "IgnoreCase", value)
tf = endsWith(str, pat, nameValuePairs...)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
str | Any | Yes | — | Text input (string/char/cell/string array). |
pat | Any | Yes | — | Pattern text (string/char/cell/string array). |
ignoreCase | Any | Yes | false | Logical flag controlling case-sensitive matching. |
name | StringScalar | Yes | "IgnoreCase" | Option name (`"IgnoreCase"`). |
value | Any | Yes | — | Option value for `"IgnoreCase"`. |
nameValuePairs... | Any | Variadic | — | Name-value option pairs (`"IgnoreCase"`, value). |
Returns
| Name | Type | Description |
|---|---|---|
tf | LogicalArray | Logical result indicating whether each text element ends with the pattern. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:endsWith:InvalidInput | Text or pattern input is not a supported text container. | endsWith: text and pattern inputs must be text values |
RunMat:endsWith:InvalidOption | IgnoreCase option arguments are invalid or malformed. | endsWith: invalid option arguments |
RunMat:endsWith:ShapeMismatch | Text and pattern inputs are not broadcast-compatible. | endsWith: input sizes are not broadcast-compatible |
RunMat:endsWith:InternalError | Internal logical result assembly failed. | endsWith: internal error |
How endsWith works
- Accepts text inputs as string scalars/arrays, character vectors/arrays, or cell arrays of character vectors.
- Accepts patterns in the same formats; scalar inputs expand across the other argument following MATLAB broadcast rules.
- Missing string scalars (
<missing>) never match any pattern. - Empty patterns (
""or'') always match non-missing text elements. - Patterns that are
<missing>never match and therefore returnfalse. - Character arrays treat each row as an independent element; zero-row character arrays yield empty outputs.
- The optional
IgnoreCaseflag can be supplied either as a trailing scalar or via the'IgnoreCase', valuename-value pair. It accepts logical/numeric scalars and the strings'on','off','true', and'false'(default is case-sensitive). - Returns a logical scalar when the broadcasted size is one element, otherwise returns a logical array.
Does RunMat run endsWith on the GPU?
endsWith performs host-side suffix comparison. When inputs currently live on the GPU, RunMat gathers them back to the host before evaluation so the behaviour is identical to MATLAB. No acceleration provider hooks are required for this builtin.
GPU memory and residency
You usually do NOT need to call gpuArray yourself in RunMat (unlike MATLAB).
endsWith always executes on the host, but RunMat's runtime automatically gathers any GPU-resident inputs before evaluating the suffix check. Because the builtin registers a ResidencyPolicy::GatherImmediately, the planner gathers device handles eagerly and computes the logical result on the CPU. You may still call gpuArray manually for compatibility with MATLAB code; the runtime gathers the inputs just in time, so results match MATLAB precisely.
Examples
Check if a filename ends with an extension
tf = endsWith("report.pdf", ".pdf")Expected output:
tf = logical
1Perform a case-insensitive suffix test
tf = endsWith("RunMat", "MAT", 'IgnoreCase', true)Expected output:
tf = logical
1Apply a scalar suffix to every element of a string array
labels = ["alpha" "beta" "gamma"];
tf = endsWith(labels, "ma")Expected output:
tf = 1×3 logical array
0 0 1Use element-wise suffixes with implicit expansion
names = ["hydrogen"; "helium"; "lithium"];
suffixes = ["gen"; "ium"; "ium"];
tf = endsWith(names, suffixes)Expected output:
tf = 3×1 logical array
1
1
1Test suffixes for a cell array of character vectors
C = {'Mercury', 'Venus', 'Mars'};
tf = endsWith(C, 's')Expected output:
tf = 1×3 logical array
0 1 1Provide multiple suffixes as a column vector
tf = endsWith("saturn", ['n'; 'x'; 'r'])Expected output:
tf = 3×1 logical array
1
0
0Handle empty patterns and missing values
texts = ["", "<missing>"];
tf = endsWith(texts, "")Expected output:
tf = 1×2 logical array
1 0Using endsWith with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how endsWith changes the result.
Run a small endsWith example, explain the result, then change one input and compare the output.
FAQ
What types can I pass to endsWith?⌄
Use string scalars/arrays, character vectors/arrays, or cell arrays of character vectors for both arguments. Mixed combinations are accepted, and RunMat performs MATLAB-style implicit expansion when the array sizes differ.
How do I ignore letter case?⌄
Supply 'IgnoreCase', true (or 'on') after the pattern argument. The option is case-insensitive, so 'ignorecase' also works. The default is false, matching MATLAB.
What happens with empty patterns?⌄
Empty patterns ("" or '') always match non-missing text elements. When the text element is missing (<missing>), the result is false.
Can I provide multiple suffixes at once?⌄
Yes. Provide pattern as a string array, character array, or cell array of character vectors. RunMat applies implicit expansion so that scalar inputs expand across the other argument automatically.
How are missing strings treated?⌄
Missing string scalars (displayed as <missing>) never match any pattern and produce false in the result. Use ismissing if you need to handle missing values separately.
Does endsWith run on the GPU?⌄
No. The builtin executes on the CPU. If inputs reside on the GPU (for example, after other accelerated operations), RunMat gathers them automatically so behaviour matches MATLAB.
Does endsWith preserve the input shape?⌄
Yes. The output is a logical array whose shape reflects the MATLAB-style implicit expansion result. When that shape contains exactly one element, the builtin returns a logical scalar.
Related Strings functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how endsWith is executed, line by line, in Rust.
- View the source for endsWith in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.