MATLAB Answers

0

how to use multiple colums to make graph (scatter) in Matlab

Asked by Maleeha Khalid Khan on 10 Sep 2019 at 19:09
Latest activity Commented on by Walter Roberson
on 14 Sep 2019 at 17:15
I want to make scatter plot suh that on left side plot using x1 and y1 values to make 1 point on left side and similar using x2 and y2 make a point on right side, Kindly help me with the code
for each row I want two points in graph
I have to do this process for the whole file ,
thanks

  1 Comment

What do you mean by 'left side' and 'right side'?
The image in your question has two sets of (x,y) coordinates. How would you like them to be separated? Two different axes?

Sign in to comment.

1 Answer

Answer by SuperNova on 10 Sep 2019 at 21:52
Edited by SuperNova on 10 Sep 2019 at 21:52

I'm guessing that you want two plots on the same graph, and that you'd use different y axes to make a point. This answer assumes that is true.
First you need to import your data:
filename = 'YourData.csv';
delimiterIn = ',';
headerlinesIn = 1;
data = importdata(filename,delimiterIn,headerlinesIn); %you may want to export the Excel document as a .txt file first, those are easier
Then define your variables:
x1 = data(:,2); %the second column in your screenshot is the x1
y1 = data(:,3);
x2 = data(:,5);
y3 = data(:,6);
Now make your plot:
yyaxis left
plot(x1,y1) %you can also use scatter or whatever kind of graph you want to make
yyaxis right
plot(x2,y2)
yyaxis left
title('Plots with Different y-Scales')
xlabel('X')
ylabel('Y1')
yyaxis right
ylabel('Y2')
Also look here for more specifics: https://www.mathworks.com/help/matlab/creating_plots/plotting-with-two-y-axes.html

  30 Comments

When I compare that situation to the data you presented an image of, it looks to me as if p1 x and p1 y are scalars. If you plot those (and y2) in one plot, then you would only get a single dot, not a line. It also looks to me like p2 x is not the same as p1 x ?
yes you are right its like this p1 and p3 hv different x value and y value but I want to plot them in same graph
for r = 1 : size(y1,1)
thisfile = sprintf('plot_%03d.jpg', r);
thisx1 = x1(r,:);
thisy1 = y1(r,:);
thisx2 = x2(r,:);
yyaxis left
h1 = plot(thisx1, thisy1, 'r*');
xlabel('x')
ylabel('y1')
yyaxis right
h2 = plot(thisx1, thisy2, 'b^');
ylabel('y2')
legend([h1, h2], {'x1', 'x2'});
title( sprintf('row #%d', r) )
pause(1) %give it time to render
saveas(gcf, thisfile)
end

Sign in to comment.