Color matching legend with variable number of data sets

1 次查看(过去 30 天)
I am attempting to create a legend for a variable (input) number of data sets. These data sets are plotted point-by-point because I need to be able to run an animation (showing the orbits of planetary bodies). This is what I have so far:
Legend=cell(N,1);
figure(1)
hold all
for k=1:N
for i=1:length(t1)-1
h_plot(k,i)=plot3([rplot(i,6*k-5); rplot(i+1,6*k-5)],[rplot(i,6*k-4),rplot(i+1,6*k-4)],[rplot(i,6*k-3),rplot(i+1,6*k-3)],'.','Color',ColorSpace(k,:));
end
Legend{k}=legend(strcat('Body ', num2str(k)));
end
xlabel('x position')
ylabel('y position')
zlabel('z position')
legend(h_plot,Legend)
I thought I could have one variable h_plot be a plot handler and Legend be a list of the body names (their index #). Then I combine the two variables into one legend() command.
I get this error output:
"Operands to the and && operators must be convertible to logical scalar values.
Error in legend (line 198) elseif narg > 0 && ~ischar(varargin{1}) && ..."
  2 个评论
Edward
Edward 2014-8-25
I actually solved the problem, but I will leave this up as an example for anyone else facing similar problems. The updated functional code is:
Legend=cell(N,1);
figure(1)
hold all
for k=1:N
for i=1:length(t1)-1
h_plot(k,i)=plot3([rplot(i,6*k-5); rplot(i+1,6*k-5)],[rplot(i,6*k-4),rplot(i+1,6*k-4)],[rplot(i,6*k-3),rplot(i+1,6*k-3)],'.','Color',ColorSpace(k,:));
end
%Legend{k}=legend(strcat('Body ', num2str(k)));
Legend{k}=strcat('Body ', num2str(k));
end
xlabel('x position')
ylabel('y position')
zlabel('z position')
legend( h_plot(:,1) ,Legend)
I changed Legend to be a list of strings instead of legend handlers. I also modified the legend() command right above to refer specifically to the plot handler indexes.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by