View all functions

CategoryCells: Core

What does the cellfun function do in MATLAB / RunMat?

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 does the cellfun function behave in MATLAB / 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.

cellfun GPU Execution Behaviour

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

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]}    {'MATLAB:UndefinedFunction @ 2'}    {[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.

See Also

cell, cell2mat, mat2cell, gpuArray, gather

Source & Feedback