MATLAB: How to replace the NaN with 0 in a cell array that has the following anatothe 10×1 cell Each cell is of size 10173 x 2, (dates and values)

arraycellcellfunisnanMATLABnan

How to replace the NaN with 0 in a cell array that has the following anatomy  10×1 cell  Each cell is of size 10173 x 2, (dates and values)

Best Answer

Please refer to the below example which shows replacing 'nan' with '0' in a cell array:
Here, 'a' is a 2x2 cell array:
a = { [5 5 5 6 ] [ nan nan nan nan]; [ 7 8 8 8 ] [1 2 3 5] };
a =
2×2 cell array
[1×4 double] [1×4 double]
[1×4 double] [1×4 double]
Create a function to replace nan with a given value as shown below:
function matrix = replace_nan(matrix, value)
matrix(isnan(matrix)) = value;
end
Now use cellfun function as shown below:
z = cellfun(@replace_nan, a, repmat( {value}, size(a,1), size(a,2)) , 'UniformOutput', 0);
'nan' values are replaced by '0' as shown below:
[5,5,5,6]    [0,0,0,0]
[7,8,8,8]    [1,2,3,5]
Also, please refer to the below links which gives more information on using 'isnan' and 'cellfun' functions.