RunMat
GitHub

compose — Format values into string arrays using printf-style placeholders in MATLAB and RunMat.

compose(formatSpec, A1, ..., An) substitutes data into % placeholders and returns a string array. It combines sprintf-style formatting with string-array expansion behavior matching MATLAB and RunMat.

Syntax

S = compose(formatSpec)
S = compose(formatSpec, A...)

Inputs

NameTypeRequiredDefaultDescription
formatSpecAnyYesFormat text or array returned as strings when no data args are supplied.
formatSpecAnyYesFormat template text.
A...AnyVariadicValues substituted into formatSpec placeholders.

Returns

NameTypeDescription
SAnyFormatted string array output.

Errors

IdentifierWhenMessage
RunMat:compose:InvalidFormatSpecformatSpec is not valid text input for compose formatting.compose: invalid formatSpec
RunMat:compose:ArgumentMismatchData arguments are not scalar or broadcast-compatible with formatSpec.compose: format data arguments must be scalars or match formatSpec size
RunMat:compose:InternalErrorInternal string-array construction failed.compose: internal error

How compose works

  • formatSpec must be text: a string scalar, string array, character vector, character array, or cell array of character vectors.
  • If formatSpec is scalar and any argument array has more than one element, RunMat broadcasts the scalar specification over the array dimensions.
  • When formatSpec is a string or character array with multiple elements, the output has the same shape as the specification. Each element uses the corresponding row or cell during formatting.
  • Arguments can be numeric, logical, string, or text-like cell arrays. Non-text arguments are converted using MATLAB-compatible rules (logical values become 1 or 0, complex numbers use the a + bi form).
  • When you omit additional arguments, compose(formatSpec) simply converts the specification into a string array, preserving the original structure.
  • Errors are raised if argument shapes are incompatible with the specification or if format specifiers are incomplete.

Does RunMat run compose on the GPU?

compose is a residency sink. When inputs include GPU-resident tensors, RunMat gathers the data back to host memory using the active acceleration provider before performing the formatting logic. All formatted strings live in host memory, so acceleration providers do not need compose-specific kernels.

Examples

Formatting A Scalar Value Into A Sentence

msg = compose("The answer is %d.", 42)

Expected output:

msg = "The answer is 42."

Broadcasting A Scalar Format Spec Over A Vector

result = compose("Trial %d", 1:4)

Expected output:

result = 1×4 string
    "Trial 1"    "Trial 2"    "Trial 3"    "Trial 4"

Using A String Array Of Formats

spec = ["max: %0.2f", "min: %0.2f"];
values = compose(spec, [3.14159, 0.125])

Expected output:

values = 1×2 string
    "max: 3.14"    "min: 0.12"

Formatting Each Row Of A Character Array

C = ['Row %02d'; 'Row %02d'; 'Row %02d'];
idx = compose(C, (1:3).')

Expected output:

idx = 3×1 string
    "Row 01"
    "Row 02"
    "Row 03"

Combining Real And Imaginary Parts

Z = [1+2i, 3-4i];
txt = compose("z = %s", Z)

Expected output:

txt = 1×2 string
    "z = 1+2i"    "z = 3-4i"

Using A Cell Array Of Format Specs

specs = {'%0.1f volts', '%0.1f amps'};
readings = compose(specs, {12.6, 3.4})

Expected output:

readings = 2×1 string
    "12.6 volts"
    "3.4 amps"

Formatting GPU-Resident Data

G = gpuArray([10 20 30]);
labels = compose("Value %d", G)

Expected output:

labels = 1×3 string
    "Value 10"    "Value 20"    "Value 30"

Using compose with coding agents

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

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

FAQ

What happens if the number of format arguments does not match the placeholders?

RunMat raises compose: format data arguments must be scalars or match formatSpec size. Ensure that each placeholder has a corresponding value or broadcast the specification appropriately.

Can compose handle complex numbers?

Yes. Complex numbers use MATLAB's canonical a + bi formatting, so %s specifiers receive the string form of the complex scalar.

How does compose treat logical inputs?

Logical values are converted to numeric 1 or 0 before formatting so they work with %d, %i, or %f placeholders.

Does compose modify the shape of the output?

No. The output matches the broadcasted size between formatSpec and the input arguments. Scalar specifications broadcast across non-scalar arguments.

What if I pass GPU arrays?

Inputs that reside on the GPU are automatically gathered to host memory before formatting. The resulting string array always lives on the CPU.

How do I emit literal percent signs?

Use %% inside formatSpec just like sprintf. The formatter converts %% into a single %.

Can I mix scalars and arrays in the arguments list?

Yes, as long as non-scalar arguments all share the same number of elements or match the size of formatSpec. Scalars broadcast across the target shape.

What happens when formatSpec is empty?

compose(formatSpec) returns an empty string array with the same shape as formatSpec. When formatSpec and arguments have zero elements, the output is 0×0.

Open-source implementation

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