RunMat
GitHub

cellfun — Apply a function to every element of one or more cell arrays, mirroring MATLAB's cellfun behaviour.

cellfun maps a function over one or more cell arrays. Each cell's contents become inputs to the supplied function handle (or builtin name), and the results are collected either into a numeric/logical/complex array ('UniformOutput', true, the default) or into a new cell array ('UniformOutput', false). RunMat follows MATLAB's semantics for result collection, broadcasting extra arguments, the 'ErrorHandler' hook, and the 'UniformOutput' toggle.

How cellfun works in RunMat

  • All cell array inputs must share the same size. Extra arguments that are **not** cell arrays are treated as constants and forwarded to every invocation.
  • The callback may be supplied as a function handle (@sin), an anonymous function, or one of MATLAB's short string identifiers (for example 'isempty' or 'isclass').
  • With 'UniformOutput', true (default) the callback must return scalars (numeric, logical, or complex). The results are packed into an array that mirrors the cell array shape. Mixed real/complex outputs automatically promote to complex.
  • With 'UniformOutput', false the callback may return arbitrary values; RunMat stores them in a new cell array that matches the input dimensions.
  • 'ErrorHandler', handler intercepts runtime errors. When the callback throws, RunMat calls handler(errStruct, cellValue1, cellValue2, ..., extraArg1, extraArg2, ...). The structure contains the fields identifier, message, index (1-based linear index), and indices (1×N vector of subscripts). The handler's return value is inserted into the result.
  • GPU values inside the cells are gathered to host memory before the callback runs so that uniform outputs can be materialised in CPU tensors today.

How cellfun runs on the GPU

cellfun itself is a host operation. RunMat gathers GPU-resident inputs before calling the callback and stores the outputs on the host. This guarantees deterministic behaviour even when the callback returns GPU arrays. Future acceleration hooks can elide the gather step when providers support nested callbacks, but no provider work is required for correctness today.

Examples

Computing string lengths across a cell array

C = {'apple', 'banana', 'cherry'};
lengths = cellfun(@length, C)

Expected output:

lengths =
     5     6     6

Mapping across two cell arrays in lockstep

A = {1, 2, 3};
B = {4, 5, 6};
totals = cellfun(@plus, A, B)

Expected output:

totals =
     5     7     9

Keeping non-scalar outputs as cells

names = {'Ada', 'Linus', 'Katherine'};
upper = cellfun(@upper, names, 'UniformOutput', false)

Expected output:

upper =
  1×3 cell array
    {'ADA'}    {'LINUS'}    {'KATHERINE'}

Supplying additional arguments alongside each cell

C = {magic(3), eye(3)};
diag3 = cellfun('size', C, 2)

Expected output:

diag3 =
     3     3

Handling callback failures with 'ErrorHandler'

C = {'42', 'NaN', '3.14'};
handler = @(err, value) NaN;
numbers = cellfun(@str2double, C, 'ErrorHandler', handler)

Expected output:

numbers =
   42     NaN    3.1400

Using builtin short names instead of function handles

cells = {[], 1:5, zeros(0, 3)};
empty_flags = cellfun('isempty', cells)

Expected output:

empty_flags =
     1     0     1

Returning rich metadata in the error handler

cells = {1, 'two', 3};
eh = @(err, value) sprintf('%s @ %d', err.identifier, err.index);
results = cellfun(@(x) x + 1, cells, 'UniformOutput', false, 'ErrorHandler', eh)

Expected output:

results =
  1×3 cell array
    {[2]}    {[117 120 112]}    {[4]}

FAQ

Does cellfun modify the original cell array?

No. The builtin reads the cell contents, calls the supplied function, and returns a new array. The original cell array is never mutated.

What happens when the callback returns mixed real and complex values?

RunMat promotes the entire output to a complex array when 'UniformOutput', true. Real results are converted to complex numbers with zero imaginary parts to match MATLAB.

Can the callback return strings or structs?

Yes, but you must specify 'UniformOutput', false. That tells RunMat to collect results into a cell array rather than forcing scalar concatenation.

Are GPU arrays supported?

Yes. Inputs that live on the GPU are automatically gathered before the callback executes so the function can operate on host values. The final result is host-resident today.

How are errors reported to 'ErrorHandler'?

The handler receives a structure with identifier, message, index, and indices fields followed by the values that triggered the error. Returning a value from the handler lets execution continue; rethrowing propagates the failure just like MATLAB.

Do extra non-cell arguments need to be the same size as the cells?

No. Extra arguments are treated as constants and forwarded unchanged for every element. Only the cell array inputs must agree on size.

Can cellfun be used with anonymous functions that capture variables?

Absolutely. Captures are forwarded to the generated closure before the cell elements, so anonymous functions behave the same way they do in MATLAB.

What are the default output types for empty inputs?

When the cell arrays are empty and 'UniformOutput', true, RunMat returns an empty double array (size matches the input). For 'UniformOutput', false, the result is an empty cell array.

Does cellfun support MATLAB's 'isclass' short name?

Yes. RunMat maps 'isclass' to the class builtin internally so you can write cellfun('isclass', C, 'double') just like in MATLAB.

What does cellfun do in MATLAB?

cellfun applies a function to each cell of a cell array and returns the collected results. For example, cellfun(@length, C) returns the length of each cell's contents.

How is cellfun different from arrayfun?

cellfun operates on cell arrays, while arrayfun operates on numeric or logical arrays. Use cellfun when your data is stored in cells (e.g., variable-length strings), and arrayfun for uniform numeric data.

Can cellfun return non-uniform outputs?

Yes. Set 'UniformOutput' to false to collect results into a cell array when the function returns different sizes or types across cells.

These functions work well alongside cellfun. Each page has runnable examples you can try in the browser.

cell, cell2mat, mat2cell, gpuArray, gather, cellstr

Open-source implementation

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

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.

  • Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
  • Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
  • A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.

Getting started · Benchmarks · Pricing

Try RunMat — free, no sign-up

Start running MATLAB code immediately in your browser.