trendlineequationcoeffnum=length(fittedlinecrossplot);
switch trendlineequationcoeffnum
case 2
trendlineequationstring=sprintf('y=%.3f x+%.3f',fittedlinecrossplot(1),fittedlinecrossplot(2));
case 3
trendlineequationstring=sprintf('y=%.3f x^2+%.3f x+%.3f',fittedlinecrossplot(1),fittedlinecrossplot(2),fittedlinecrossplot(3));
case 4
trendlineequationstring=sprintf('y=%.3f x^3+%.3f x^2+ %.3f x+%.3f',fittedlinecrossplot(1),fittedlinecrossplot(2),fittedlinecrossplot(3),fittedlinecrossplot(4));
case 5
trendlineequationstring=sprintf('y=%.3f x^4+%.3f x^3+ %.3f x^2+%.3f x+%.3f',fittedlinecrossplot(1),fittedlinecrossplot(2),fittedlinecrossplot(3),fittedlinecrossplot(4),fittedlinecrossplot(5));
end
htrendlinetext=text(pos_rightsubplot(1)+0.5*pos_rightsubplot(3),pos_rightsubplot(2)+0.8*pos_rightsubplot(4),trendlineequationstring,'units','normalized');
htrendlinetext.FontSize=12;
htrendlinetext.FontWeight='bold';
htrendlinetext.Color='r';
Best Answer