Limit values based on one column?

Hi. I'm trying to plot data inputted from Excel into a simple (x,y) scatter plot.
As an example, data in the Excel file was:
Location Distance (m) Time (seconds)
1 5 120
1 10 200
1 25 300
1 30 350
2 4 100
2 50 200
2 70 250
2 100 300
3 5 75
3 12 100
3 13 200
3 40 300
I've already successfully inputted the data into a matrix using:
B = xlsread('filename','sheetname');
I set each column as a variable:
L = B(:,1);
D = B(:,2);
T = B(:,3);
I'd like to plot (D,T) for each value of L. I know how to do this if I call each cell in L; for example:
for x = D(1:4)
for y = T(1:4)
scatter(x,y)
end
end
However, I'd like to set it up so that I can call for the location (ie, for L = 1) and create separate (x,y) plots of each L, as I have quite a bit of data and think this will be faster. Additionally, the L values correspond with a text value (I converted to numbers as the text appeared as NaN). If possible, I'd like to make the title of each graph correspond with the text Location value. Any suggestions for how to approach this are greatly appreciated.
Cheers

 Accepted Answer

Something like this?
u = unique(B(:,1));
for k=1:numel(u)
d = B(B(:,1)==u(k),:);
figure; scatter(d(:,2),d(:,3)); grid on;
title(sprintf('Location = %d',u(k)));
end

5 Comments

DrWooo
DrWooo on 2 Oct 2017
Edited: DrWooo on 2 Oct 2017
Thanks for the response.
This successfully created distinct plots with corresponding titles (Location = 1, Location = 2, etc.).
However, the data is now plotting a straight line along the y-axis. It appears the data was sorted and that each plot is corresponding with a range of x-values (ie, x-axis range for Location = 1 is 0:100, x-axis range for Location = 2 is 100:200, etc) instead of with the actual data at that Location value.
"... the data is now plotting a straight line along the y-axis ..."
I don't understand. The code I posted, using the data you posted, gives individual scatter plots with small circles at each data point. There are no lines plotted. Are you talking about what the code does for a different set of data? Is B still an Nx3 matrix?
DrWooo
DrWooo on 2 Oct 2017
Edited: DrWooo on 2 Oct 2017
By line, I mean the general line that the scatter plot is making -- apologies for not being more clear.
Yes, I am using a different, larger dataset, with a similar positive slope for each of the Locations. B is a 53x18 double matrix (versus the 12x3 in the above example).
I've attached examples of the plots for clarity -- the first plot is of my actual dataset using the original code posted (with a lsline); the second and third plots are how the code you shared is running.
Thanks
So, what is the relationship between the columns of your 53x18 matrix and your 12x3 example? How are we supposed to know what you are wanting to plot if your example does not match your actual data?
I've fixed the issue -- I failed to adjust the code you shared to the actual corresponding column in my data. The code works great.
Thanks for your assistance and patience.

Sign in to comment.

More Answers (0)

Asked:

on 2 Oct 2017

Commented:

on 2 Oct 2017

Community Treasure Hunt

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

Start Hunting!