RunMat
GitHub

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

NameTypeRequiredDefaultDescription
strAnyYesInput text scalar/array/cell.
startAnyYesStart boundary marker text or positive integer position(s).
endAnyYesEnd boundary marker text or positive integer position(s).
NameStringScalarYesOption name (`Boundaries`).
ValueAnyVariadicOption value and additional Name/Value pairs.

Returns

NameTypeDescription
newTextAnyText with between-boundary content erased, preserving text container semantics.

Errors

IdentifierWhenMessage
RunMat:eraseBetween:InvalidInputFirst 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:BoundaryTypeStart/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:PositionTypeNumeric boundary positions are not positive finite integers.eraseBetween: position arguments must be positive integers

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 startPos and endPos are 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 missing placeholder in either boundary or text yields <missing> in the result). When a boundary cannot be located, eraseBetween returns 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.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how eraseBetween is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.