MATLAB: How to plot multiple multcompare plots from anova

anovanmultcompareplottiledlayout

It doesn't work when I try to use tiledlayout to make a combined plot
% Loading data
y = [52.7 57.5 45.9 44.5 53.0 57.0 45.9 44.0]';
g1 = [1 2 1 2 1 2 1 2];
g2 = {'hi';'hi';'lo';'lo';'hi';'hi';'lo';'lo'};
g3 = {'may';'may';'may';'may';'june';'june';'june';'june'};
% For loop with subplot
for n = 1:4
subplot(2,2,n)
hold on
[~,~,stats] = anovan(y,{g1 g2 g3},'model','interaction','varnames',{'g1','g2','g3'});
results = multcompare(stats,'Dimension',[1 2]);
hold off
end
And tiledlayout also doesn't work
tiledlayout('flow')
nexttile
[~,~,stats] = anovan(y,{g1 g2 g3},'model','interaction','varnames',{'g1','g2','g3'});
results = multcompare(stats,'Dimension',[1 2]);
nexttile
[~,~,stats] = anovan(y,{g1 g2 g3},'model','interaction','varnames',{'g1','g2','g3'});
results = multcompare(stats,'Dimension',[1 2]);

Best Answer

  • multcompare() calls meansgraph(), which always unconditionally does a clf() which erases the entire current figure.
    Therefore to plot multiple such graphs on the same figure, you are going to need to provide a figure for multcompare() to scribble into, and then you are going to have to extract the axes from the figure and move it to the desired target location.
    However, there is the hidden difficulty that the graphs are interactive, each line having a buttondownfcn callback. To support that, the axes is given UserData and also the figure is given UserData as well. If you had more than one kind of multcompare, such as mixing anova1 with kruskalwallis then you would have a conflict when you merged the axes into one figure, but you should be okay as long as they are all for the same kind of graph.