I exported data from excel into a table, how do I sum the values of each column from that table excluding NaN's

2 views (last 30 days)
T = readtable('Inventory.xlsx')
%now I need to sum each column, for example the data in column 2 is for Copper

Answers (1)

Star Strider
Star Strider on 20 May 2022
Two options —
T = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1005700/Inventory.xlsx')
T = 8×9 table
Var1 Copper Steel Plastic Cement Glass Brick Wood Paper ________________ ______ _____ _______ ______ _____ _____ ____ _____ {'Pretoria' } 105 212 554 1047 42 8750 447 NaN {'Johannesburg'} 121 256 320 1245 58 9462 213 NaN {'Cape Town' } 110 280 415 1623 62 6445 NaN NaN {'Durban' } 92 110 NaN 994 44 2125 897 7500 {'Bloemfontein'} 64 NaN NaN 426 12 3144 54 NaN {'Polokwane' } 81 40 NaN 677 NaN 8789 78 1244 {'East London' } 42 NaN NaN 120 20 1897 NaN 687 {'Mbombela' } 80 51 NaN 188 22 560 22 1268
S1 = nansum(T{:,2:end})
S1 = 1×8
695 949 1289 6320 260 41172 1711 10699
S2 = sum(T{:,2:end},'omitnan')
S2 = 1×8
695 949 1289 6320 260 41172 1711 10699
Both give the same result.
.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by