How to fill blank cells in the table using filled cells in the same column?

17 views (last 30 days)
I have a 1*125 cell named "C". inside C I have 125 tables (456*12 tables). I want to fill every blank cell in 1 to 4 columns of these tables using the value written in the cells in the same column. please look at this example to explain what I want:
column 1 column2
blank blank
24 red
24 red
blank blank
24 red
24 red
24 red
so I want to change the above table to this:
column 1 column2
24 red
24 red
24 red
24 red
24 red
24 red
24 red
I want to apply this for every 125 tables in the C (column 1 to 4) I don't want to change the rest of the columns.
I was attached to C for you.
if any other question you have I would answer so quick.
thank you all.

Accepted Answer

Adam Danz
Adam Danz on 14 Jan 2020
Edited: Adam Danz on 14 Jan 2020
This simple, easy-to-read loop fills the missing elements in the first 4 columns of each table with the nearest non-missing element and takes less than 0.3 seconds.
for i = 1:numel(C)
C{i}(:,1:4) = fillmissing(C{i}(:,1:4),'nearest');
end
If the header names for the 4 key columns are the same for all of your tables (which is the case of your data), you can use this method which uses variable names rather than assuming the column arragement of your tables.
headers = {'station_id' 'station_name' 'region_id' 'region_name'};
for i = 1:numel(C)
colIdx = ismember(C{i}.Properties.VariableNames, headers);
C{i}(:,colIdx) = fillmissing(C{i}(:,colIdx),'nearest');
end
Both methods require Matlab >=2016b
More info on fillmissing().

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!