Is wrapping the function with the {} operator a valid replacement of 'UniformOutput', false in cellfun?

Viewed 428

I am using cellfun to apply a function to each cell in a cell array.

I know that I must set 'UniformOutput' to false whenever the function returns non-scalar values, so that the outputs of the function are returned encapsulated in a cell array.

Take the following cell array as an example:

C1 = {[1 2 3], [4 5 6]};

C1 has two cells and each cell contains a vector of three elements:

C1 =

  1×2 cell array

    [1×3 double]    [1×3 double]

If I want to add 1 to the contents in every cell, I can define the function @(x) x + 1 and apply it using cellfun as follows:

C2 = cellfun(@(x) x + 1, C1, 'UniformOutput', false);

This works very well, but note that I need to make sure that 'UniformOutput' is set to false as I explained before, otherwise an error would be thrown.

However, after reading this thread, I realized that if I wrap the function with the cell array construction operator {} like this @(x) {x + 1} then I don't need to set 'UniformOutput' to false.

So the following command will generate the same results as in C2 without throwing any errors:

C3 = cellfun(@(x) {x + 1}, C1);

In terms of code layout I prefer this approach since it is more compact and less verbose than the former, but I am not sure if this is always safe.


Thus my question is:

Can I always wrap the function with {} to avoid setting 'UniformOutput' to false? Or are there any scenarios where such replacement would not work?


My research:

help cellfun

'UniformOutput' -- a logical value indicating whether or not the output(s) of FUN can be returned without encapsulation in a cell array. If true (the default), FUN must return scalar values that can be concatenated into an array. If true, the outputs must be of the following types: numeric, logical, char, struct, cell. If false, cellfun returns a cell array (or multiple cell arrays), where the (I,J,...)th cell contains the value FUN(C{I,J,...}, ...). When 'UniformOutput' is false, the outputs can be of any type.

The following fragment is part of an answer to a related question:

[...] cellfun takes care of the dereference operation which is required to do detailed operations on individual elements of a cell when looping (that is, the {}) [...]

2 Answers
Related