I understand that you are facing issues while trying to use the function "grpstats". The syntax for using the "grpstats" function is:
statarray = grpstats(tbl,groupvar,whichstats)
This syntax finds summary statistics specified in "whichstats" based on grouping variable "groupvar" for the data specified in "tbl". Thus, to find the mean, maximum and minimum MPG for cars manufactured in each year in the "carbig" dataset you have to use the following commands:
meanMPG = grpstats(MPG,Model_Year,'mean');
maxMPG = grpstats(MPG,Model_Year,'max');
minMPG = grpstats(MPG,Model_Year,'min');
One way to visualize the MPG statistics by year is to use "errorbar". This function has the following syntax:
Thus, for the dataset mentioned in this question, "Y" is the mean MPG which is plotted with respect to "X" or the Model_Year. The advantage of using this function is that the error "E" can be replaced with the standard deviation which is then plotted as an error bar. here is some code to do the same:
stdMPG = grpstats(MPG,Model_Year,'std');
errorbar(unique(Model_Year),meanMPG,stdMPG);
xlabel('Model year');
ylabel('MPG');
title('MPG over Time');
Best Answer