MATLAB: Finding the percentage of NaN cells in columns

MATLABnan

I have an excel file. I want to know how much (%) of my data are NaN.
I want to found that for columns 9 to 12 separately, and have the results something like this:
I attached my excel file. this is just a sample and I have more than 125 excel files which I gonna do this for all. any help is appreciated.
thank you all

Best Answer

This does everything for all the tables in ‘C’:
D = load('C.mat');
C = D.C;
for k = 1:numel(C)
C{k}(:,1:4) = fillmissing(C{k}(:,1:4),'nearest'); % Fills Missing Data
filename = C{k}{1,2}; % Input Table Name
fn{k,:} = sprintf('%s.xlsx',filename{:}); % Output File Name
writetable(C{k},sprintf('%s.xlsx',filename{:})) % Write Each Table To Separate File
rowlen{k,:} = size(C{k}(:,9:12),1); % Row Lengtth Of Each Table
NaN912{k,:} = [varfun(@isnan,C{k}(:,9:12))]; % Number Of ‘NaN’ Values In Each Table
PctNaN(k,:) = table2array(varfun(@(x)sum(x)./size(x,1),NaN912{k,:})); % Percent ‘NaN’ Values In Selected Variables (Columns)
end
VarNms = compose('NAN in %s', string(C{1}.Properties.VariableNames(:,9:12))); % Variable Names For ‘NaNPercent’ Table
filnam = cell2table(fn,'VariableNames',{'File name'}); % File Names Table
NaNPercent = array2table(PctNaN, 'VariableNames',VarNms); % ‘PctNaN’ Initial Table
NaNPercent = [filnam NaNPercent]; % ‘PctNaN’ Final (Output) Table
FirstTen = NaNPercent(1:10,:) % Display Sample (Delete)
Producing:
FirstTen =
10×5 table
File name NAN in tmax_m NAN in tmin_m NAN in rrr24 NAN in tm_m
_______________________ _____________ _____________ ____________ ___________
{'Abadan.xlsx' } 0.088235 0.1299 0.13971 0.1152
{'Abadeh.xlsx' } 0.10539 0.13235 0.1299 0.19118
{'Abali.xlsx' } 0.12162 0.12162 0.15315 0.15766
{'Abumusa Island.xlsx'} 0.1464 0.14865 0.11486 0.16892
{'Ahar.xlsx' } 0.21171 0.2027 0.21396 0.24099
{'Ahvaz.xlsx' } 0.051471 0.058824 0.058824 0.056373
{'Aligudarz.xlsx' } 0.1982 0.22523 0.23198 0.23874
{'Anar.xlsx' } 0.22297 0.18919 0.19369 0.23649
{'Arak.xlsx' } 0.068627 0.078431 0.088235 0.093137
{'Ardakan (Yazd).xlsx'} 0.21114 0.20882 0.34339 0.2065
The code retains the original data in ‘rowlen’ and ‘NaN912’ as well as the percent NaN results in the ‘NaNPercent’ table.