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

1 view (last 30 days)
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
Adam Danz
Adam Danz on 10 Sep 2019
Edited: Adam Danz on 12 Sep 2019
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.

Answers (1)

SuperNova
SuperNova on 10 Sep 2019
Edited: SuperNova on 10 Sep 2019
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
Maleeha Khalid Khan
Maleeha Khalid Khan on 14 Sep 2019
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
Walter Roberson
Walter Roberson on 14 Sep 2019
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.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!