Legend and for loops
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/155274/image.jpeg)
Hey, I am trying to evaluate some data and show it in a graph consisting of 3 sets each containg Mean values, x-errorbars and a fit function (See picture). But I am only able to get a part of the legend showing the right color for the last itteration or I get the whole legend but the color of my 'mean values' is set to that of the last itteration (symbol is ok). I tried numerus approaches but nothing seems to work. My latest try is this code ():
for (3 times itteration)...... other stuff goin on.....
[h,hh]= errorbar_x_one(eval_complete(2,:),eval_complete(1,:),eval_complete(3,:)); %errorbar_x_one outputs handle of errorbar and handle of scatter
y_loudness_function = loudness_function(x_achse,eval_line_point);%loudness function fit
hhh = plot(x_achse,y_loudness_function,[col_sym.col{col_sym.xxx} ':']);
grid on
col_sym.xxx = col_sym.xxx+1;
h.DisplayName = 'Errorbar';
hh.DisplayName = ['Mean ' condition{col_sym.xxx}] ;
hhh.DisplayName = 'fit';
end
legend([h hh hhh],'Location','NW')
Any help would be greatly appreciated.
0 comentarios
Respuestas (1)
Mike Garrity
el 17 de Feb. de 2016
You're saying you call
legend([h hh hhh], ...)
each time around the loop? That means that each time you're telling legend to forget about the items from the previous iteration and focus on these 3 new objects.
You'll want to move legend out of the loop. You could do something like this:
g = gobjects(1,9);
for i=1:3
[h, hh] = errorbar_x_one( ...
hhh = plot( ...
g(end+1) = h;
g(end+1) = hh;
g(end+1) = hhh;
end
legend(g)
But if you're really putting everything into the legend, then you don't need to give it the handles. So you may be able to do something as simple as this:
for i=1:3
[h, hh] = errorbar_x_one( ...
hhh = plot( ...
end
legend show
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!