What does the eraseBetween function do in MATLAB / RunMat?
eraseBetween(text, start, stop) removes the portion of text that appears between two boundary
markers. Boundaries can be substrings or numeric positions. The builtin mirrors MATLAB semantics,
including support for string arrays, character arrays, cell arrays, implicit expansion, and the
'Boundaries' name-value argument that toggles inclusive or exclusive behaviour.
How does the eraseBetween function behave in MATLAB / RunMat?
- Accepts string scalars, string arrays, character arrays (row-by-row), and cell arrays containing string scalars or character vectors; the output keeps the same container type.
- Boundary arguments can be text or numeric positions. Both boundaries in a call must use the same representation—mixing text and numeric markers raises a size/type error.
- Text boundaries are exclusive by default: the markers are preserved while the enclosed text is
deleted. Numeric positions are inclusive by default: characters at
startPosandendPosare deleted together with the interior. 'Boundaries','inclusive'removes the markers themselves;'Boundaries','exclusive'keeps them. The option is case-insensitive and must be supplied as name-value pairs.- Missing string scalars propagate (the MATLAB
missingplaceholder in either boundary or text yields<missing>in the result). When a boundary cannot be located,eraseBetweenreturns the original element unchanged. - Numeric positions are validated as positive integers, clamped to the string length, and interpreted using MATLAB’s 1-based indexing rules.
eraseBetween Function GPU Execution Behaviour
The builtin performs all work on the CPU. When any argument is GPU-resident, RunMat gathers the
values first, applies the deletions on the host, and returns host-resident outputs. Providers do
not need to expose device kernels, and fusion planning treats eraseBetween as a residency sink so
surrounding expressions will gather automatically.
Examples of using the eraseBetween function in MATLAB / RunMat
Removing text between substrings
txt = "The quick brown fox";
result = eraseBetween(txt, "quick", " fox");
Expected output:
result = "The quick fox"
Deleting substrings across a string array
str = ["The quick brown fox jumps"; "over the lazy dog"];
starts = ["quick"; "the"];
ends = [" fox"; " dog"];
trimmed = eraseBetween(str, starts, ends);
Expected output:
trimmed = 2×1 string
"The quick fox jumps"
"over the dog"
Removing characters between numeric positions
name = "Edgar Allen Poe";
short = eraseBetween(name, 6, 11);
Expected output:
short = "Edgar Poe"
Using inclusive boundaries to drop the markers
sentence = "The quick brown fox jumps over the lazy dog";
collapsed = eraseBetween(sentence, " brown", "lazy", "Boundaries", "inclusive");
Expected output:
collapsed = "The quick dog"
Operating on character arrays while preserving padding
chars = char("Server<GPU>", "Engine<CPU>");
trimmed = eraseBetween(chars, "<", ">", "Boundaries", "inclusive");
Expected output:
trimmed =
2×6 char array
"Server"
"Engine"
Preserving element types in cell arrays
C = {'alpha<1>', "beta<2>";
'gamma<3>', "delta<4>"};
clean = eraseBetween(C, "<", ">", "Boundaries", "inclusive");
Expected output:
clean =
2×2 cell array
{'alpha'} {"beta"}
{'gamma'} {"delta"}
Handling missing strings safely
texts = [missing, "Planner<GPU>"];
result = eraseBetween(texts, "<", ">");
Expected output:
result = 1×2 string
"<missing>" "Planner"
GPU residency in RunMat (Do I need gpuArray?)
No. RunMat automatically gathers device-resident inputs, performs the deletion on the CPU, and
returns host outputs. Manual gpuArray / gather calls are unnecessary; they are honoured only for
compatibility with MATLAB when you explicitly need to control residency.
FAQ
Which argument types does eraseBetween accept?
The first argument can be a string scalar, string array, character array, or cell array of character vectors / string scalars. Boundary arguments must both be text markers or both be numeric positions.
What happens if a boundary is not found?
The original text is returned unchanged. Missing string scalars also propagate unchanged.
How does 'Boundaries','inclusive' interact with text markers?
Inclusive mode removes the matched start and end markers together with the enclosed text. Exclusive mode keeps the markers and removes only the interior.
Can I broadcast scalar boundaries across an array input?
Yes. Scalar markers follow MATLAB implicit expansion rules. Character-array and cell-array markers must match the size of the text input.
Are GPU inputs supported?
GPU values are gathered to host memory before processing. The builtin always returns host-resident outputs and is registered as an Accelerate sink, so fusion planning does not keep text on the GPU.
Does eraseBetween validate numeric positions?
Yes. Positions are parsed as positive integers using MATLAB’s 1-based indexing. Stops are clamped to the string length, and start positions that lie beyond the text leave the element unchanged.
See Also
extractBetween, erase, replace, split, join
Source & Feedback
- Implementation:
crates/runmat-runtime/src/builtins/strings/transform/erasebetween.rs - Found an issue? Please open a GitHub issue with a minimal reproduction.