Creating legend from fittype function

3 views (last 30 days)
Ran Kagan
Ran Kagan on 30 May 2022
Commented: Ran Kagan on 30 May 2022
Hi,
I've been trying to create legends for numerous curves I plotted (using fittype/fit functions) on the same graph, but with no success.
All "legend" functions didn't seem to work like I wanted. See my code below with my "%"s as my trial and erros.
Would apprecaite your help in this matter!
Thanks.
x_alpha = [0,2,5];
SF_alpha=[1, 0.0505, 0.02971];
SF_alpha_TMZ=[0.4347, 0.03565, 0.01657];
x_gamma = [0,2,5];
SF_gamma=[1,0.43571,0.07857];
SF_gamma_TMZ=[0.67142,0.35428,0.04642];
% Alpha
g=fittype('exp1');
myfit=fit(x_alpha',SF_alpha',g)
hL1=plot(myfit, x_alpha, SF_alpha)
xlim([0 5.5])
hold on
myfit1=fit(x_alpha',SF_alpha_TMZ',g)
hL2=plot(myfit1, x_alpha, SF_alpha_TMZ)
% Gamma
m=fittype('exp(-a*x-b*x^2)');
myfit2=fit(x_gamma',SF_gamma',m)
hL3=plot(myfit2, x_gamma, SF_gamma)
myfit3=fit(x_gamma',SF_gamma_TMZ',m)
hL4=plot(myfit3, x_gamma, SF_gamma_TMZ)
hold off
set([hL1(2)],'color','k')
set([hL2(2)] ,'color','b')
set([hL3(2)] ,'color','m')
set([hL4(2)] ,'color','r')
%h=[hL1;hL2;hL3;hL4];
%legend(h,'Alpha SF','Alpha SF+TMZ','Gamma SF','Gamma SF+TMZ');
%legend([hL1;hL2;hL3;hL4], 'Alpha SF','Alpha SF+TMZ','Gamma SF','Gamma SF+TMZ')
%legend(h, {'Alpha SF','Alpha SF+TMZ','Gamma SF','Gamma SF+TMZ'}, 'Location', 'BestOutside')
set(gca, 'YScale', 'log')

Answers (1)

KSSV
KSSV on 30 May 2022
Instead of this line:
hL1=plot(myfit, x_alpha, SF_alpha) ;
use:
xi = linspace(min(x_alpha),max(x_alpha)) ;
hL1=plot(xi,myfit(xi), x_alpha, SF_alpha)
And use legend.
  1 Comment
Ran Kagan
Ran Kagan on 30 May 2022
Hi,
I tried using your suggested lines but it draws linear lines rather than using the "exp1" and the second exponent function I wanted.
I suppose there's a method to create the legend simply by using the legend syntax correctly without changing the code itself?

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by