MATLAB: Cellfun with empty cell input

cellfun empty cellMATLAB

When I use cellfun with an empty cell as input, the output is an empty array. Is that a bug in matlab? How can that be avoided?
If i do x = arrayfun(@(i){y(i)},t),
and t is not an empty array, then x is a cell array and if t is empty, then x is an empty array which i can understand.
but if i do x = cellfun(@(i)y(i),num2cell(t)), i would expect x to be a cell always. no matter what t is.
Is it possible to always have x a cell array without checking if it is empty?

Best Answer

x = cellfun(@fn, c)
x = arrayfun(@fn, a)
x will never be a cell array regardless of input cell array c or array a, unless function fn returns a cell array. Thus with,
x = cellfun(@(i) y(i), num2cell(t))
unless y(i) is a cell array, x will never be a cell array.
To force cellfun and arrayfun to return cell arrays, set 'UniformOutput' to false:
x = cellfun(@(i) y(i), num2cell(t), 'UniformOutput', false)