Clear Filters
Clear Filters

Fill NAN values in a table

16 views (last 30 days)
Sarah Yun
Sarah Yun on 15 Dec 2019
Commented: Image Analyst on 16 Dec 2019
Hi,
I have a table that consists of 5 columns
One column, temperature, displays some NAN values
I would like to use the inpaint_nans function to fill the NAN by interpolation
How do I fill in the temperature column only?
Then save the table as new table after NAN filled?
Thank you.

Accepted Answer

Image Analyst
Image Analyst on 15 Dec 2019
Try this well commented example I created for you:
% Create data because the user forgot to post any!
% Make random values for Date, temp, humidity, pressure, precip, depth
numRows = 50;
temperature = randi(100, [numRows, 1]);
humidity = randi(100, [numRows, 1]);
pressure = randi(100, [numRows, 1]);
precip = randi(100, [numRows, 1]);
depth = randi(100, [numRows, 1]);
% Make some of the temperature elements nan
temperature(10:20) = nan;
indexes = randperm(length(temperature), 8); % 8 other random locations.
temperature(indexes) = nan;
% Create a table, T, as a container for the workspace variables.
T = table(temperature, humidity, pressure, precip, depth)
%------------------------------------------------------------------------
% % Now we have our table and we can begin.
% Now fix the nans. First extract the temperature column.
temperature = T.temperature
% Find nan locations (rows)
nanRows = isnan(temperature)
% Sometimes, either the first or last element(s) are nan, and the interpolation will leave a nan there.
% So to prevent that we have to find the first non-nan element and tack it onto the beginning,
% and find the last non-nan element and tack it on to the end.
indexes = find(~nanRows)
repairedTemperature = [temperature(indexes(1)); temperature; temperature(indexes(end))]; % Make sure to use semicolon rather than comma.
% Find nan locations (rows) again with the "fixed" array.
nanRows = isnan(repairedTemperature)
% Interpolate non-nan locations
repairedTemperature = interp1(find(~nanRows), repairedTemperature(~nanRows), 1:length(repairedTemperature))
% Now truncate off the first and last elements we tacked on. Also need to transpose since interp1() gives a row vector.
repairedTemperature = repairedTemperature(2:end-1)';
% Stick the results back into the table.
T.temperature = repairedTemperature;
fprintf('All done with demo by Image Analyst.\n');
  2 Comments
Sarah Yun
Sarah Yun on 15 Dec 2019
Edited: Sarah Yun on 15 Dec 2019
This is great, thanks Image Analyst.
Question:
If I want to ignore NANs instead of interpolate (and put ignored values back in table), will this work - or maybe it will cause problems for future analysis?
Thank you.
Image Analyst
Image Analyst on 16 Dec 2019
I don't know. If you want to extract all non-nan rows, you can do this:
temperature = T.temperature
% Find nan locations (rows)
nanRows = isnan(temperature)
temperature = temperature(~nanRows);
Whether that shortened list causes problems is something you would know, not me. Depends on how you're going to use them. There is no need to put the "ignored" values back in the table since they never left the table. If you want a whole table without the nan rows, I think you can do this
T = T(~nanRows, :); % Remove rows where temperature is nan from the table.

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!