MATLAB: How to write a function that finds the max, min, mean, median, mode, std, var, and count of an array? call it “thestat”

functionmatlab functionmean

function [min,max,mean,median,mode,var,std] = mystat(x) % Enter in an array and mystat will give the min, max, mean, median, mode, % var, and standard deviation of the array.
mean = mean(x) median = median(x) mode = mode(x) var = var(x) std = std(x) count = length(x) end
For some reason this code doesn't work when I try to call it.

Best Answer

By specifying your output arguments using the same names as the functions that you need to call to compute those outputs, you make it so MATLAB cannot call those functions. [You can't use the same name as a function and as a variable inside a function.] Change the name of the output arguments, or pack all those outputs into a struct array so that the user of your function doesn't need to call it with seven separate outputs (and doesn't need to remember the order in which you defined your function to return those outputs.)
myout = struct;
myout.mean = mean(x); % etc
Related Question