putting a legend to plot a data in a for loop

3 visualizaciones (últimos 30 días)
Meva
Meva el 2 de Jun. de 2015
Editada: Cindy Solomon el 3 de Jun. de 2015
Hello,
I have :
if l==1
thickness = bodyfunction1;
end
if l==2
thickness = bodyfunction2;
end
if l==3
thickness = bodyfunction3;
end
if l==4
thickness =bodyfunction4;
end
...... I want to plot a data with indexes from i=1 to 4:
for i=1:4
plot(t_vect(1:t_clashing_index(i)-20), H1Rarray(1:t_clashing_index(i)-20, i));
hold on;
legend('with body shape =',num2str(i),...'location','Best');
plot(t_vect(1:t_clashing_index(i)-20), H1Larray(1:t_clashing_index(i)-20, i));
hold on;
end
The question is : How can I distinguish H1Rarray and H1Larray with four different body thickness functions?
How can I put legend so that the program can understand if i=1 H1Rarray shows this is for bodyfunction1, if i=2 H1Rarray shows this is for bodyfunction2 , ... and the same thing for H1Larray ??
Many thanks
  1 comentario
Sriram Narayanan
Sriram Narayanan el 3 de Jun. de 2015
The two sections of code that you have shared is not clear enough. You are assigning the "thickness" variable to the corresponding body function, but, are plotting t_clashing_index.
I would have to look at the code structure, but, assuming that the four body functions are MATLAB functions that return the body thickness function, you could have a function that accepts the desired value of "l" as input and within that function, the for-loop could immediately follow the if statement once the desired value of "l" has been selected by the user.

Iniciar sesión para comentar.

Respuestas (1)

Cindy Solomon
Cindy Solomon el 3 de Jun. de 2015
Editada: Cindy Solomon el 3 de Jun. de 2015
Hi Meva,
Could you please clarify what your desired outcome would be? I wasn't quite sure if you wanted 8 legend entries that would specify something like "H1RArray, BodyStyle X" or if you only wanted 4 legend entries (one for each "body thickness") with some other way of distinguishing between the two arrays without having an entry in the legend, or something else entirely.
If you wanted the 4 thicknesses to have a different plot style, you could change the call to your plot to be something like:
plot(t_vect(1:t_clashing_index(i)-20),H1Rarray(1:t_clashing_index(i)-20, plotStyle{i})
such that there would be a unique color/marker combination for each of the 4 thicknesses that you defined earlier. Ex:
plotStyle = {'b','k','r.','g0', ...};
Within your for loop, you could then add something like:
legendInfo{i} = ['Body Shape = ' num2str(i)];
and then outside the loop add the command:
legend(legendInfo)
Is there a reason why you have to use a 'for' loop? Vectorizing your code would be more efficient, and you could more easily do something like the example below that shows different colors for each thickness and different marker types for each array. However, in doing so, the color of the marker would not be the same as any of the body thicknesses, so this might be confusing for an observer.
ax = gca;
xData = plot(magic(4),'Marker','o');
hold on
ax.ColorOrderIndex = 1;
yData = plot(magic(4)+5,'Marker','.');
ax.ColorOrderIndex = 1;
pColors = plot(1,[1 1 1 1]);
pStyles(1) = plot(1,1,'o');
pStyles(2) = plot(1,1,'.');
legend([pColors; pStyles'],{'Color 1','Color 2','Color 3','Color 4','Style 1','Style 2'});
set([pColors; pStyles'],'Visible','off');
Hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by