MATLAB: Replace NaN’s in table with zero

basicbeginnercellformatmatrixnanremovereplacereplacementtableszeros

Hello, I have a 1501×7 table called 'x' and there appears to be NaN's in the fourth and sixth column called "Age" and "height". I would like a way to replace NaN's with zeros. Take note, that I have already tried:
k = find(isnan(x))';
x(k) = 0;
% and
x(isnan(x)) = 0;
Yet, neither work because I am using a table, not a matrix. I have also tried converting my table into a cell array, and using these same functions, but they still do not work. They return:"Undefined function 'isnan' for input arguments of type 'cell'" ALSO, please note that the table has columns full of text. So, cell2mat does not work.

Best Answer

There's a function called standardizeMissing that would replace a non-NaN value with NaN, but normally, replacing NaN with a constant value (as opposed to, for example, some sort estimated value) would be kind of a funny thing to do. I'll assume you have a good reason.
Either of the following should work:
>> t = table({'smith';'jones';'doe'},[20;NaN;40],[NaN;72;66],[120;130;140],'VariableNames',{'Name' 'Age' 'Height' 'Weight'})
t =
Name Age Height Weight
_______ ___ ______ ______
'smith' 20 NaN 120
'jones' NaN 72 130
'doe' 40 66 140
>> vars = {'Age' 'Height'};
>> t2 = t{:,vars};
>> t2(isnan(t2)) = 0;
>> t{:,vars} = t2
t =
Name Age Height Weight
_______ ___ ______ ______
'smith' 20 0 120
'jones' 0 72 130
'doe' 40 66 140
>> t = table({'smith';'jones';'doe'},[20;NaN;40],[NaN;72;66],[120;130;140],'VariableNames',{'Name' 'Age' 'Height' 'Weight'});
>> [~,vars] = ismember({'Age' 'Height'},t.Properties.VariableNames)
vars =
2 3
>> for i=vars, t.(i)(isnan(t.(i))) = 0; end
Hope this helps.