1
votes

I have 2 datasets and a line :

for example :

dt1X = [1:30];
dt1Y = [1:30];

dt2X = [1:30];
dt2Y = [0.5:0.5:15.0];

line = [2 1]; % y= 2 + x;

I want to draw these two dataset and line in same graph with different colours.How can I do that?

I tried the line below to change a color but it gave this error : invalid value for array property "cdata"

hold on;
scatter(dt1X,dt1Y,[],"r"); %this line give error
scatter(dt2X,dt2Y,[],"g");
hold off;

EDIT:

    hold on;
    scatter(dt1X,dt1Y,[],[],"r");
    scatter(dt1X,dt1Y,[],[],"g");
    hold off;

Result : No error but both datasets draw with blue dots...

using "r" and 'r' Didn't change anythinh it works same in both situation.

2
In Matlab, replace double quotes (") by single quotes ('). Other than that, I get no errors - Luis Mendo
Try another [], before the "r". - David
works for me with Octave 3.7.7+ (I don't have the stable version around to test). - carandraug
@David by adding another [] parameter I gave no error but. It draws the two dataset same colour(blue). - Berke Cagkan Toptas
@LuisMendo I try (") and (') but it doesn't change the function behaviour. - Berke Cagkan Toptas

2 Answers

0
votes

To clear the figure first add

clf

before

hold on;
scatter(dt1X,dt1Y,[],[],"r");
scatter(dt1X,dt1Y,[],[],"g");
hold off;

then you will see that nothing is plotted. So replace these lines by

hold on;
scatter(dt1X,dt1Y,"r");
scatter(dt1X,dt1Y,"g");
hold off;
0
votes

I would also rearrange your code like this:

dt1X = [1:30];
dt1Y = dt1X;

line = [2 1]; % y= 2 + x;

dt2X = dt1X;
dt2Y = line(1) + (line(2) * dt2X); % y= 2 + x;

clf
hold on
plot(dt1X,dt1Y,'r');
plot(dt2X,dt2Y,'g');