I have a 2-by-54 cell array in Matlab. Occasionally, the second row contains some Inf
values. I'd like to convert all Inf
values to NaN
values and tried using this code:
dataC(cellfun(@isinf, dataC, 'UniformOutput', false)) = {NaN};
where dataC
is my cell array.
However, when doing that I get the following error:
Error using subsindex
Function 'subsindex' is not defined for values of class 'cell'.
How can I solve this problem?
To use the approach you proposed in the question, remove the
UniformOutput
-property, this way a logical index is returned and the elements can be addressed directly. This works only when the cell content is scalar!Edit: Thanks @Luis Mendo for mentioning, that the use of
find
is not needed here. My original solution has beendataC(find(cellfun(@isinf,dataC))) = {NaN}
and used the unnecessaryfind
in the addressing ofdataC
.In case you have arrays in the cells, use the following approach: Write your own function and provide it as a function handle. In the following code I implemented the function
replaceInf
to do the replacement.This gives the following output: