How to get mean of all variables in a table?
84 views (last 30 days)
Show older comments
Hello, A table ('kub') that I am trying to analyse has a dimension of 1882x33. Its a mixed table with double and strings values. The 1st and 2nd variable is contains strings. Therefore, I used the following line of code to get the mean of all Variables except 1st and 2nd.
func = @mean;
B = varfun(func,kub{:,3:end});
However, I am getting the following error.
Check for missing argument or incorrect argument data type in call to function 'varfun'.+
Can anyone help me?
0 Comments
Accepted Answer
Steven Lord
on 9 Apr 2021
The varfun function requires one of its inputs to be a table or timetable array (or a tall table or timetable.)
which -all varfun
In your call the first input is a function handle. When you index into a table or timetable array using curly braces what you receive is not itself a table or timetable.
T = array2table(magic(4))
smallerTable = T(2:3, 2:4) % table array
doublePiece = T{2:3, 2:4} % double array
If you want to compute the mean of all the elements of the doublePiece variable don't use varfun.
M1 = mean(doublePiece, "all")
M2 = mean(doublePiece, 1)
M3 = mean(doublePiece, 2)
But if you want to operate on just the numeric variables in your table, you can use a function handle to select as the input variables only those that are numeric.
T.stringvar = ["a"; "b"; "c"; "d"]
varfun(@mean, T, 'InputVariables', @isnumeric)
More Answers (0)
See Also
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!