MATLAB: How to assign a NAN to an empty cell in a cell array of cell array of matrix

cell operations

I want to set NAN to these cells. Basically i wanted to do a basic threshold operation on a cell array of cell array of matrix . Some of the cells are empty cells, while others have values. If the absolute values in the cells are less than a threshold value say 5, i want to assign a zero to it. The problem is that the empty cells are also set to zero, which i do not want. So if we convert empty cell to NAN this problem can be solved. After the operation i can reassign NAN as an empty cell. But I am not able to perform this operation. I am able to do it in the first level based on previous discussion in the forum. for eg:
c={[] [] [2 4 5] [] [2 3 4 6 7]}% 1×5 cell array -- [] [] [1×3 double] [] [1×5 double]
fx=@(x)any(isempty(x))
ind=cellfun(fx,c)
c(ind)={nan}
I get
c =
1×5 cell array
[NaN] [NaN] [1×3 double] [NaN] [1×5 double]
Now for this example how can i do the same process?
c={{[] [1 2]} { [] [] [2 4 5]} {[] [2 3 4 6 7] []}}
c =
1×3 cell array
{1×2 cell} {1×3 cell} {1×3 cell}
When i follow the previous step I am getting the error "Function 'subsindex' is not defined for values of class 'cell'."

Best Answer

As per James' comment it would probably be more useful to fix your thresholding code so it works on empty cells as well.
Also note that in your example, the any is useless and fx could just be @isempty.
To answer your immediate question, you will have to use a loop:
for idx = 1:numel(c)
c{idx}(cellfun(@isempty, c{idx})) = {nan};
end
Or if you really want to use cellfun for the outer cell array, you will have to use a named function (in a file) as anonymous functions can't do assignment:
function c = empty2nan(c) %named function
c(cellfun(@isempty, c)) = {nan}
end
c = cellfun(@empty2nan, c, 'UniformOutput', false) %cellfun for outer cell array
The explicit loop is simpler, imo.