MATLAB: How to plot the results of two curve fitting operations on two different Y axes as in PLOTYY

Curve Fitting Toolboxfit

I would to use PLOTYY to plot the results of two curve fitting operations. This is useful when I want to plot data with two different scales on the same axis.
I would like to be able to execute the following code
plotyy(cfout1, cfout2)
where both 'cfout1' and 'cfout2' are objects of type'cfit'.

Best Answer

The ability to plot multiple 'cfit' objects using PLOTYY is not available in MATLAB 7.3 (R2006b).
To work around this issue, you can manually create multiple axes and have the data plotted on them after evaluating the fitted function at a series of points along the x-axis. This is shown in the following example:
clear all; close all
%%Load data
load census
%%define fitting options
s = fitoptions('Method','NonlinearLeastSquares',...
'Lower',[0,0],...
'Upper',[Inf,max(cdate)],...
'Startpoint',[1 1]);
f = fittype('a*(x-b)^n','problem','n','options',s);
f2 = fittype('poly1');
%%create second data set
pop2 = log(pop);
%%Fit data
[c2,gof2] = fit(cdate,pop,f,'problem',2);
[c3,gof3] = fit(cdate,pop2,f2);
%%Construct axes
low_lim = min(cdate);
hi_lim = max(cdate);
x = linspace(low_lim, hi_lim, 100);
y1 = feval(c2, x);
y2 = feval(c3, x);
figure(1); clf
% first axis
hl1 = plot(cdate, pop, 'ro');
hold on;
hl2 = plot(x, y1, 'c');
legend(hl2, 'fitted curve')
ax1 = gca;
set(ax1,'XColor','r','YColor','r');
% second axis
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k');
gca = ax2;
hold on;
hl3 = plot(cdate, pop2, 'bs');
hold on;
hl4 = plot(x, y2, 'b-');
legend([hl2 hl4], 'fitted curve', 'fitted curve')
% adjust scale and horizontal axis limits
axis(ax2, [low_lim hi_lim min(y2) max(y2)])
set(ax2, 'xlim', get(ax1, 'xlim'));