I can't figure why this legend is not working

4 views (last 30 days)
When I was trying to plot a whole bunch of data I couldn't get the legend to work. Can someone help me get the legend to work? I'm able to plot the data, but for some reason all of the legends after the first are all black stars.
figure;
plot(ff)
hold on;
plot(V_LOCS,0,'k*')
plot(vp,0.9,'bo')
plot(vn,-0.9,'ro')
plot(vpf,1,'go')
plot(vnf,-1,'go')
plot(Vp_LOCS,1,'b.')
plot(Vn_LOCS,-1,'r.')
hold off;
legend('frontal','frontal peak','vp','vn','vpf','vnf','Vp Location','V Location')
grid on;
Ok I found that matlab thinks that there are many data points for each plot. So I changed the code to this
figure;
plot(ff)
hold on;
plot(V_LOCS,zeros(length(V_LOCS),1),'k*')
plot(vp,zeros(length(vp),1)+0.9,'bo')
plot(vn,zeros(length(vp),1)-0.9,'ro')
plot(vpf,ones(length(vpf),1),'go')
plot(vnf,-ones(length(vnf),1),'go')
plot(Vp_LOCS,ones(length(Vp_LOCS),1),'b.')
plot(Vn_LOCS,-ones(length(Vn_LOCS),1),'r.')
hold off;
legend('frontal','frontal peak','vp','vn','vpf','vnf','Vp Location','V Location')
grid on;
But is there a better way of plotting like this without all of the zeros() and ones() ?

Answers (1)

Geoff Hayes
Geoff Hayes on 24 Apr 2015
Alex - you could use repmat to produce the second input to your calls to plot. Something like
plot(V_LOCS,repmat(0,size(V_LOCS)),'k*')
or
plot(vp,repmat(0.9,size(vp)),'bo')
In either of the above we replicate the 0 or 0.9 to create an array that has the same dimensions as V_LOCS or vp. It is a little cleaner than using zeros or ones because we specify the value that we want to replicate rather than having to manipulate the results of the other two functions.
Try the above and see what happens!

Products

Community Treasure Hunt

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

Start Hunting!