Color matching legend with variable number of data sets

2 views (last 30 days)
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 Comments
Edward
Edward on 25 Aug 2014
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.
Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh on 25 Aug 2014
Good Job.
Post your comment as an answer and then accept it.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!