Plot legend with multiple fitresult curves

16 views (last 30 days)
curoi
curoi on 15 Mar 2013
Edited: Fc Fc on 9 Feb 2018
I fitted three separate Gaussian curves to my x and y data using the 'Generate Code' option in the Curve fit toolbox. I then copied and pasted some of the code and changed it according to my needs. My question is how do you get the legend to display only the first, third, and fourth plots specified as h1, h3, and h4 respectively given multiple 'fitresult' plots?
The second plot, h2, specifies the same Gaussian 1 fitresult as the first plot, h1, except that I wanted to be able to extend the curve fit beyond the data extent. When I try to use the following code, I get an error stating "Error using horzcat: CAT arguments dimensions are not consistent."
%%Plot model fits for BOWB
% Plot fit with data.
figure(2);
hold off;
cla;
hold on;
h1 = plot( (fitresult{1}), 'r-', gxData, gyData, 'bo');
h2 = plot( (fitresult{1}), 'r-' );
h3 = plot( (fitresult{2}), 'g-' );
h4 = plot( (fitresult{3}), 'm-' );
% Label axes
ylim( [ 0, 11.5e-3 ] );
xlim( [ 24000, 42000] );
set( gca,'XTick',( 24000:1800:42000 ));
set( gca,'XTickLabel',round( 400:30:700 ));
legend( [h1 h3 h4], 'Sample data', 'Gaussian 1 fit', 'Gaussian 2 fit', ...
'Gaussian 3 fit' );
The code above works fine if I only have the first plot, similar to what the 'Generate Code' feature in the curve fit toolbox creates. The problem is I want to show multiple curve fits on the same plot while simultaneously have the first curve fit extend much further beyond the data range. Any ideas?

Answers (2)

ChristianW
ChristianW on 16 Mar 2013
x = 1:10;
y = sin(x);
f = fit(x',y','cos(x+a)');
h1 = plot(x,y,'ok'); hold on
h2 = plot(f);
X = 1:0.1:20; % extended x
h3 = plot(X,f(X),':g');
legend([h1 h2 h3],'data','fit','fit extended')

Fc Fc
Fc Fc on 9 Feb 2018
Edited: Fc Fc on 9 Feb 2018
Hi, I solved in this way
fitresults{1}=myfitga1;
fitresults{2}=myfitga2;
figure (5)
plot5=figure (5);
set(plot5, 'Visible', 'off');
hold on;
plot((fitresults{1}),'r-',eVcIpicco,ckxfitIpicco,'b*');
legend_Ipicco{1}='Dati';
legend_Ipicco{2}='Gaussian fit';
plot((fitresults{2}),'g-');
legend_Ipicco{3}='Gaussian 2 fit';
xlabel('Energia (eV)');
ylabel('Conteggi relativi');
legend(legend_Ipicco);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!