Legend ommit first plot title in multiple plots

I am attempting to create a comparison plot between the original fuzzified data and a fitted version. However, the label for the first plot ('Data #1') is missing from the legend. I have tried adjusting the position of the 'hold on' command, but have been unable to resolve the issue."
clf, figure('Position',[1 1 600 300],'Units','pixels');
hold on
for i=1:length(uCenters)
fo = fitoptions( ...
'Method','NonlinearLeastSquares', ...
'StartPoint',[1 uCenters(i)], ...
'Lower',[0 -Inf]);
ft = fittype('gaussmf(x,[a,c])','options',fo);
f = fit(uSrt,uFCM(i,:)',ft);
H1 = plot(uSrt',uFCM(i,:),'--g');
H1.DisplayName = sprintf('Data #%d',i);
H2 = plot(f,'b');
H2.DisplayName = sprintf('Fuzzy #%d',i);
end
hold off, grid on, legend('Location','eastoutside','Box','off')
xlim([min(uSrt) max(uSrt)])
ylim([0 1.1])
xlabel('Voltaje (V)'), ylabel('Nivel de pertenencia')
title('Voltaje fuzzificado ajustado a curva gaussiana')

1 comentario

dpb
dpb el 20 de Mayo de 2023
Without data to check against, it's hard to debug exactly, but I'd venture that the first data set are NaN.
Your code also overwirtes the plot and fit handles every pass through the loop so you can't inspect the results of each loop independently after the loop completes; only by setting breakpoint and using the debugger as is. It would be better to save an array of handles, then you could see each result more simply...

Iniciar sesión para comentar.

 Respuesta aceptada

First, reproducing the problem using random data:
figure('Position',[1 1 600 300],'Units','pixels')
hold on
for i=1:5
x = rand(10,1);
y = rand(10,1);
fo = fitoptions( ...
'Method','NonlinearLeastSquares', ...
'StartPoint',[1 i], ...
'Lower',[0 -Inf]);
ft = fittype('gaussmf(x,[a,c])','options',fo);
f = fit(x,y,ft);
H1 = plot(x,y,'--g');
H1.DisplayName = sprintf('Data #%d',i);
H2 = plot(f,'b');
H2.DisplayName = sprintf('Fuzzy #%d',i);
end
hold off, grid on, legend('Location','eastoutside','Box','off')
xlabel('Voltaje (V)'), ylabel('Nivel de pertenencia')
title('Voltaje fuzzificado ajustado a curva gaussiana')
You can see that 'Data #1' is missing from the legend, as in your case. I'm not sure what causes that, but storing the line handles - as dpb suggested - and specifying them explicitly in the legend call seems to fix it:
figure('Position',[1 1 600 300],'Units','pixels')
hold on
for i=1:5
x = rand(10,1);
y = rand(10,1);
fo = fitoptions( ...
'Method','NonlinearLeastSquares', ...
'StartPoint',[1 i], ...
'Lower',[0 -Inf]);
ft = fittype('gaussmf(x,[a,c])','options',fo);
f = fit(x,y,ft);
H1(i) = plot(x,y,'--g');
H1(i).DisplayName = sprintf('Data #%d',i);
H2(i) = plot(f,'b');
H2(i).DisplayName = sprintf('Fuzzy #%d',i);
end
hold off, grid on, legend(reshape([H1; H2],[],1),'Location','eastoutside','Box','off')
xlabel('Voltaje (V)'), ylabel('Nivel de pertenencia')
title('Voltaje fuzzificado ajustado a curva gaussiana')

Más respuestas (0)

Categorías

Productos

Versión

R2023a

Preguntada:

el 20 de Mayo de 2023

Respondida:

el 23 de Ag. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by