What does the strrep function do in MATLAB / RunMat?
strrep(str, old, new) replaces every non-overlapping instance of the substring old that appears in
str with the text provided in new. The builtin accepts string scalars, string arrays, character arrays,
and cell arrays of character vectors, matching MATLAB behaviour exactly.
How does the strrep function behave in MATLAB / RunMat?
- String scalars remain strings. Missing string values (
<missing>) propagate unchanged. - String arrays are processed element-wise while preserving their full shape and orientation.
- Character arrays are handled row by row. Rows expand or shrink as needed and are padded with spaces so the result stays a rectangular char array, just like MATLAB.
- Cell arrays must contain character vectors or string scalars. The result is a cell array of identical size where each element has had the replacement applied.
- The
oldandnewarguments must be string scalars or character vectors of the same data type. oldcan be empty. In that case,strrepinsertsnewbefore the first character, between existing characters, and after the final character.
strrep Function GPU Execution Behaviour
RunMat treats text replacement as a CPU-first workflow:
- The builtin is registered as an Accelerate sink, so the planner gathers any GPU-resident inputs (string arrays, char arrays, or cell contents) back to host memory before work begins.
- Replacements are computed entirely on the CPU, mirroring MATLAB’s behaviour and avoiding GPU/device divergence in string handling.
- Results are returned as host values (string array, char array, or cell array). Residency is never pushed back to the GPU, keeping semantics deterministic regardless of the active provider.
GPU residency in RunMat (Do I need gpuArray?)
No. strrep registers as a sink with RunMat Accelerate, so the fusion planner never keeps its inputs or
outputs on the GPU. Even if you start with GPU data, the runtime gathers it automatically—manual gpuArray
or gather calls are unnecessary.
Examples of using the strrep function in MATLAB / RunMat
Replacing a word inside a string scalar
txt = "RunMat turbo mode";
result = strrep(txt, "turbo", "accelerate");
Expected output:
result = "RunMat accelerate mode"
Updating every element of a string array
labels = ["GPU planner", "CPU planner"];
updated = strrep(labels, "planner", "pipeline");
Expected output:
updated = 2×1 string
"GPU pipeline"
"CPU pipeline"
Preserving rectangular shape in character arrays
chars = char("alpha", "beta ");
out = strrep(chars, "a", "A");
Expected output:
out =
2×5 char array
'AlphA'
'betA '
Applying replacements inside a cell array of character vectors
C = {'Kernel Fusion', 'GPU Planner'};
renamed = strrep(C, ' ', '_');
Expected output:
renamed = 1×2 cell array
{'Kernel_Fusion'} {'GPU_Planner'}
Inserting text with an empty search pattern
stub = "abc";
expanded = strrep(stub, "", "-");
Expected output:
expanded = "-a-b-c-"
Leaving missing string values untouched
vals = ["RunMat", "<missing>", "Accelerate"];
out = strrep(vals, "RunMat", "RUNMAT");
Expected output:
out = 1×3 string
"RUNMAT" <missing> "Accelerate"
Replacing substrings gathered from GPU inputs
g = gpuArray("Turbine");
host = strrep(g, "bine", "bo");
Expected output:
host = "Turbo"
FAQ
Which input types does strrep accept?
String scalars, string arrays, character vectors, character arrays, and cell arrays of character vectors.
The old and new arguments must be string scalars or character vectors of the same data type.
Does strrep support multiple search terms at once?
No. Use the newer replace builtin if you need to substitute several search terms in a single call.
How does strrep handle missing strings?
Missing string scalars remain <missing> and are returned unchanged, even when the search pattern matches
ordinary text.
Will rows of a character array stay aligned?
Yes. Each row is replaced individually, then padded with spaces so that the overall array stays rectangular, matching MATLAB exactly.
What happens when old is empty?
RunMat mirrors MATLAB: new is inserted before the first character, between every existing character, and
after the last character.
Does strrep run on the GPU?
Not today. The builtin gathers GPU-resident data to host memory automatically before performing the replacement logic.
Can I mix strings and character vectors for old and new?
No. MATLAB requires old and new to share the same data type. RunMat enforces the same rule and raises a
descriptive error when they differ.
How do I replace text stored inside cell arrays?
strrep traverses the cell array, applying the replacement to each character vector or string scalar element
and returning a cell array of the same shape.