RunMat
GitHub

strtrim — Remove leading and trailing whitespace from text inputs with MATLAB-compatible container semantics.

strtrim(text) removes leading and trailing whitespace while preserving internal spacing. It supports MATLAB-compatible string, char, and cellstr-style input containers.

Syntax

out = strtrim(str)

Inputs

NameTypeRequiredDefaultDescription
strAnyYesString/char/cell text input to trim.

Returns

NameTypeDescription
outAnyTrimmed text preserving input container kind and shape.

Errors

IdentifierWhenMessage
RunMat:strtrim:InvalidInputInput is not a string array, character array, or cell array of text scalars.strtrim: first argument must be a string array, character array, or cell array of character vectors
RunMat:strtrim:CellElementCell array contains a non-text element or non-row char array element.strtrim: cell array elements must be string scalars or character vectors
RunMat:strtrim:InternalErrorInternal output container construction failed.strtrim: internal error

How strtrim works

  • Whitespace is defined via MATLAB's isspace, so spaces, tabs, newlines, and other Unicode whitespace code points are removed from both ends of each element.
  • String scalars and arrays keep their type and shape. Missing string scalars (<missing>) remain missing and are returned unchanged.
  • Character arrays are trimmed row by row. The result keeps the original number of rows and shrinks the column count to the longest trimmed row, padding shorter rows with spaces so the output stays rectangular.
  • Cell arrays must contain string scalars or character vectors. Results preserve the original cell layout with each element trimmed.
  • Numeric, logical, or structured inputs raise MATLAB-compatible type errors.

Does RunMat run strtrim on the GPU?

strtrim runs on the CPU. When the input (or any nested element) resides on the GPU, RunMat gathers it to host memory before trimming so the output matches MATLAB exactly. Providers do not need to implement device kernels for this builtin today.

GPU memory and residency

You do not need to call gpuArray or gather manually. RunMat automatically gathers any GPU-resident text data before applying strtrim, so the builtin behaves the same regardless of where the data lives.

Examples

Trim Leading And Trailing Spaces From A String Scalar

name = "   RunMat   ";
clean = strtrim(name)

Expected output:

clean = "RunMat"

Remove Extra Whitespace From Each Element Of A String Array

labels = ["  Alpha  "; "Beta   "; "   Gamma"];
trimmed = strtrim(labels)

Expected output:

trimmed = 3×1 string
    "Alpha"
    "Beta"
    "Gamma"

Trim Character Array Rows While Preserving Shape

animals = char('  cat   ', 'dog', ' cow ');
result = strtrim(animals)

Expected output:

result =

  3×3 char array

    'cat'
    'dog'
    'cow'

Trim Tabs And Newlines Alongside Spaces

text = "\tMetrics " + newline;
clean = strtrim(text)

Expected output:

clean = "Metrics"

Trim Each Element Of A Cell Array Of Character Vectors

pieces = {'  GPU  ', " Accelerate", 'RunMat   '};
out = strtrim(pieces)

Expected output:

out = 1×3 cell array
    {'GPU'}    {"Accelerate"}    {'RunMat'}

Preserve Missing String Scalars

vals = [" ok "; "<missing>"; " trimmed "];
trimmed = strtrim(vals)

Expected output:

trimmed = 1×3 string
    "ok"
    <missing>
    "trimmed"

Using strtrim with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how strtrim changes the result.

Run a small strtrim example, explain the result, then change one input and compare the output.

FAQ

Does strtrim modify internal whitespace?

No. Only leading and trailing whitespace is removed; interior spacing remains intact.

Which characters count as whitespace?

strtrim removes code points that MATLAB's isspace recognises, including spaces, tabs, newlines, carriage returns, and many Unicode space separators.

How are character arrays resized?

Each row is trimmed independently. The output keeps the same number of rows and shrinks the width to match the longest trimmed row, padding shorter rows with spaces if necessary.

What happens to missing strings?

Missing string scalars (string(missing)) remain <missing> exactly as in MATLAB.

Can I pass numeric or logical arrays to strtrim?

No. Passing non-text inputs raises a MATLAB-compatible error indicating that text input is required.

How does strtrim differ from strip?

strtrim always removes leading and trailing whitespace. strip is newer and adds options for custom characters and directional trimming; use it when you need finer control.

What does strtrim do in MATLAB?

strtrim removes leading and trailing whitespace from a string or character array. Interior whitespace is preserved.

How is strtrim different from strip in MATLAB?

Both remove leading and trailing whitespace, but strip additionally lets you specify which side to trim ('left', 'right', or 'both') and which characters to remove.

Does strtrim work with string arrays?

Yes. strtrim operates element-wise on string arrays, removing whitespace from each element independently. Missing strings remain missing.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how strtrim 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.