HOW TO PLOT DATA BASED ON DATA IN ANOTHER COLUMN

HI!
i have 5 coloumns of data wiht over 1000 entries.
i want to plot column 4 and 5 against each other (x and y respectively) based on the value in column 1.
for example.
for length of coloumns
for value in column 1 = 1
plot corresponsponding column 4 and 5 values
for value column 1 = 2
plot corresponsponding column 4 and 5 vlaues
etc
end
i want to plot the conditions in a different colour as well.
please help :)

Answers (1)

What type of plot do you want? If a scatter plot, use gscatter.
% Create data
A=[randi(2,100,1),rand(100,1),rand(100,1)];
A=sortrows(A,2);
ind = A(:,1)==1;
% Create grouped scatter plot
gscatter(A(:,2),A(:,3),A(:,1))
If you prefer a line plot instead, use logical indexing to select your groups. Here's a simple example assuming two groups in column 1.
figure
% Use logical indexing to identify one group
ind = A(:,1)==1;
% Use 'not ==1' syntax to plot second group
plot(A(ind,2),A(ind,3),A(~ind,2),A(~ind,3))

7 Comments

hi! thanks for this. i managed to find the solution.
i am trying to simplify the code now.
is there a way to run this through a for loop?
it needs to run through column 1 until a certain value (i.e 12) read through column 3 to determine if it is 1 or 0 and plot column 4 againt column 5 based on the value in column 3 and 1...
i can do this for each case individually but i need to make the script more applicable to different files....
Share the code you have working so far.
i dont have one...
this it what i tried to do but it takes way to long. It does not give me the expected outcome as it reads all data as 0.... i have tried elseif statements, have tried everything.
% for i=1: length(COP_final)
%
% if COP_final(i,3)== 0
% c='r';
% else
% COP_final(i,3)== 1
% c='b';
% end
% gscatter(COP_final(:,4), COP_final(:,5), COP_final(:,1),c);
% legend({'left foot (red)', 'right foot (blue)'})
% title('Centre of Pressure data exported from GaitRite for each foot fall')
% xlabel('X COP values'); ylabel('Y COP values');
% set(gca, 'YDir', 'reverse');
% end
Then perhaps you could share a representative sample of your data? You can attach it using the paperclip icon.
this is what is expected. i achieved this manually. using the following code.
gscatter(COP_final(:,4), COP_final(:,5), COP_final(:,1), 'rb', '.')
legend(ax1,{'right foot (blue)', 'left foot (red)'})
title('Centre of Pressure data exported from GaitRite for each foot fall')
xlabel('X COP values'); ylabel('Y COP values');
now i am attempting to apply this same concept to any data file... i hope this makes sense.
ignore the data legend!
What isn't working? gscatter should work fine for any similarly formatted data. The only limiting input right now is the specified colors. Remove the 'rb' input, and see if your code runs for any data file. If it does, then we can worry about specifying color.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 21 Feb 2021

Commented:

on 23 Feb 2021

Community Treasure Hunt

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

Start Hunting!