How can I plot a legend only for data points?

Hello I try to plot a legend for different data points and get this error:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in Robertson86 (line 57) h(i)=plot(xdata(in),ydata(in),'o','Color',cdata2(i,:)); But it does work with line plots and fills.
for i = 1:12
% plot a polygon
Poly = Robertson_zones_1986{i};
plot(Poly(:,1),Poly(:,2),'k','LineWidth',1.5)
fill(Poly(:,1),Poly(:,2),cdata(i,:))
text(RobNumPos86(i,1),RobNumPos86(i,2),num2str(i),'Color',[0.3 0.3 0.3])
% check if data is inside this particular polygon
in= inpolygon(xdata,ydata,Poly(:,1),Poly(:,2));
h(i)=plot(xdata(in),ydata(in),'o','Color',cdata2(i,:));
b(i)=numel(xdata(in));
end
legend(h,'1.Data type','2.Data type',....)

 Accepted Answer

Your plot outputs multiple line handles. If that is not what you want, then you need to adjust the dimensions of xdata and ydata. If you want to plot multiple line objects in every iteration, then I suggest you store those values in a cell array:
h{i}=plot(xdata(in),ydata(in),'o','Color',cdata2(i,:));

11 Comments

Unable to perform assignment because brace indexing is not supported for variables of this type.
Error in Robertson86 (line 57) h{i}=plot(xdata(in),ydata(in),'o','Color',cdata2(i,:));
jonas
jonas on 9 Oct 2018
Edited: jonas on 9 Oct 2018
Well you probably forgot to clear h. Can you copy the size of h using your old code and show me?
The size of h is 1x9 Patch. And inside it is 1x1 Patch. Check photo attached.
jonas
jonas on 9 Oct 2018
Edited: jonas on 9 Oct 2018
I thought you were trying to plot a number of points? Could you perhaps upload the data?
jonas
jonas on 9 Oct 2018
Edited: jonas on 9 Oct 2018
If h is 12x1, then the code should not return an error? Again, could you perhaps upload the data so I can have a look?
Also, why is h a double? Are you using an old version of MATLAB (2018b is indicated in the question)
The problem is that you have no points inside of some of the polygons. Therefore, no line handle is produced and the script returns an error when it tries to assign an empty array to h(i). Try something like this
figure;hold on
for i = 1:12
% plot a polygon
Poly = Robertson_zones_1986{i};
plot(Poly(:,1),Poly(:,2),'k','LineWidth',1.5)
m=fill(Poly(:,1),Poly(:,2),cdata(i,:))
text(RobNumPos86(i,1),RobNumPos86(i,2),num2str(i),'Color',[0.3 0.3 0.3])
% check if data is inside this particular polygon
in= inpolygon(xdata,ydata,Poly(:,1),Poly(:,2));
if sum(in)>0;
h(i)=scatter3(xdata(in),ydata(in),ones(size(ydata(in))),[],cdata(i,:),'filled','markeredgecolor','r','DisplayName',sprintf('Data type %g',i));
b(i)=numel(xdata(in));
else
h(i)=NaN
end
end
legend(h(~isnan(h)))
I dont know how you want to deal with the fact that some data types are absent. Do you want to indicate this in the legend somehow? Also, note that I changed the 'cdata2(i,:)' that was inside of the plot to 'cdata(i,:)' as I did not find a variable called cdata2.
Thanks for the answer. I will try to implement your solution and get back to you. cdata2 is created in a code (line 34) as a random RGB colour vector for different zones where data is plotted. My goal is to show a legend of data points that appeared in any of the zones cause this code is going to be used for different data inputs so the data might appear in any of those 12 zones. As I understood from your solution my loop was problematic cause some of the data does not appear in the zones.
Exactly. An interesting observation I made was that neither of
plot([],[])
scatter([],[])
returns a handle. Therefore, if you try to assign them to h(i), they will both return an error. However, if you write like this
plot([],[],'color','r')
scatter([],[],'markerfacecolor','r')
then the second one (scatter) actually returns a handle. I don't know why this is the case. But if you want to show all datatypes, even those not present in the plot, then you can simply remove the if-condition, such as
figure;hold on
for i = 1:12
% plot a polygon
Poly = Robertson_zones_1986{i};
plot(Poly(:,1),Poly(:,2),'k','LineWidth',1.5)
m=fill(Poly(:,1),Poly(:,2),cdata(i,:))
text(RobNumPos86(i,1),RobNumPos86(i,2),num2str(i),'Color',[0.3 0.3 0.3])
% check if data is inside this particular polygon
in= inpolygon(xdata,ydata,Poly(:,1),Poly(:,2));
h(i)=scatter3(xdata(in),ydata(in),ones(size(ydata(in))),[],cdata(i,:),'filled','markeredgecolor','r','DisplayName',sprintf('Data type %g',i));
b(i)=numel(xdata(in));
end
legend(h)
Yes, your solution does work as I wished. Thanks again for your help!

Sign in to comment.

More Answers (0)

Products

Release

R2018b

Asked:

on 8 Oct 2018

Commented:

on 9 Oct 2018

Community Treasure Hunt

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

Start Hunting!