View all functions

CategoryStrings: Transform

What does the strtrim function do in MATLAB / RunMat?

strtrim(text) removes leading and trailing whitespace characters from text. The input can be a string scalar, string array, character array, or a cell array of character vectors, mirroring MATLAB behaviour. Internal whitespace is preserved exactly as provided.

How does the strtrim function behave in MATLAB / RunMat?

  • 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.

strtrim Function GPU Execution Behaviour

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 residency in RunMat (Do I need gpuArray?)

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 of using the strtrim function in MATLAB / RunMat

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"

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.

See Also

strip, upper, lower

Source & Feedback