eraseBetween — Delete text between boundary markers in MATLAB and RunMat.
eraseBetween(text, start, stop) removes text between two boundary markers. String/position markers, expansion rules, and 'Boundaries' option behavior follow MATLAB semantics.
Syntax
newText = eraseBetween(str, start, end)
newText = eraseBetween(str, start, end, Name, Value, ...)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
str | Any | Yes | — | Input text scalar/array/cell. |
start | Any | Yes | — | Start boundary marker text or positive integer position(s). |
end | Any | Yes | — | End boundary marker text or positive integer position(s). |
Name | StringScalar | Yes | — | Option name (`Boundaries`). |
Value | Any | Variadic | — | Option value and additional Name/Value pairs. |
Returns
| Name | Type | Description |
|---|---|---|
newText | Any | Text with between-boundary content erased, preserving text container semantics. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:eraseBetween:InvalidInput | First argument is not a string array, character array, or cell array of text scalars. | eraseBetween: first argument must be a string array, character array, or cell array of character vectors |
RunMat:eraseBetween:BoundaryType | Start/end boundaries are mixed text/numeric domains or use unsupported boundary types. | eraseBetween: start and end arguments must both be text or both be numeric positions |
RunMat:eraseBetween:PositionType | Numeric boundary positions are not positive finite integers. | eraseBetween: position arguments must be positive integers |
RunMat:eraseBetween:NameValuePair | Name/value options are not supplied in complete pairs. | eraseBetween: name-value arguments must appear in pairs |
RunMat:eraseBetween:OptionName | An option name other than `Boundaries` was supplied. | eraseBetween: unrecognized parameter name |
RunMat:eraseBetween:OptionValue | `Boundaries` option value is not `inclusive` or `exclusive`. | eraseBetween: 'Boundaries' must be either 'inclusive' or 'exclusive' |
RunMat:eraseBetween:CellElement | Cell text input/boundary contains non-text values or non-row char arrays. | eraseBetween: cell array elements must be string scalars or character vectors |
RunMat:eraseBetween:SizeMismatch | Text/boundary inputs are not broadcast-compatible for erase semantics. | eraseBetween: boundary sizes must be compatible with the text input |
RunMat:eraseBetween:InternalError | Internal output construction failed. | eraseBetween: internal error |
How eraseBetween works
- 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.
Does RunMat run eraseBetween on the GPU?
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.
GPU memory and residency
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.
Examples
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"Using eraseBetween with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how eraseBetween changes the result.
Run a small eraseBetween example, explain the result, then change one input and compare the output.
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.
Related Strings functions
Transform
erase · extractBetween · join · lower · pad · replace · split · strcat · strip · strjoin · strrep · strsplit · strtrim · upper
Core
char · compose · num2str · sprintf · str2double · strcmp · strcmpi · string · string.empty · strings · strlength · strncmp
Search
contains · endsWith · startsWith · strfind
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how eraseBetween is executed, line by line, in Rust.
- View the source for eraseBetween 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.