MATLAB: Question about the legend command

gscatterlegendpoint classification

Hi, I am working on the following code to plot three different classes:
% a - Plot the data in a two-dimensional vector space.
clc;clear;
% Class 1
x1 = [16; 18; 20; 11; 17; 8; 14; 10; 4; 7;];
y1 = [13; 13; 13; 12; 12; 11; 11; 10; 9; 9];
class1 = [x1 y1];
% Class 2
x2 = [8; 9; 6; 8; 5; 7; 4; 6; 4; 3];
y2 = [8; 7; 7; 6; 5; 5; 4; 3; 2; 2];
class2 = [x2 y2];
% Class 3
x3 = [19; 19; 17; 17; 16; 14; 13; 13; 11; 11];
y3 = [6; 3; 8; 1; 4; 5; 8; 1; 6; 3];
class3 = [x3 y3];
figure
gscatter(x1,y1,class1,'b')
xlabel('Band 1'), ylabel('Band 2')
title('Data of Each Class in Two-Dimensional Vector Space')
hold on
gscatter(x2,y2,class2,'r','x')
hold on
gscatter(x3,y3,class3,'g','o')
legend('Class 1', 'Class 2', 'Class 3')
I am getting what I want on the plot but the legend says Class 1, Class 2, Class 3 but it shows the point structure from class 1 on each of the classes. What can I do on the legend to show the points of each class, respectively?

Best Answer

You cannot do that. gscatter() creates one line for every group, and you have defined each point to be part of different groups, so each of your gscatter() is going to produce 10 lines. You are then trying to legend() those 30 lines total with only 3 legends.
I think you need to go back and re-read the requirements for the grouping variable; see http://www.mathworks.com/help/stats/grouping-variables.html
You would need your grouping variable to be all the same in order to have only one line per plot produced.