Three columns (x,y,z) in table, Loop through table for each X plot Y vs Z

2 views (last 30 days)
table
X Y Z
A 1 3
A 2 4
A 3 10
B 1 4
B 2 4
B 4 6
C 0 1
C 1 4
C 2 5
C 3 7
C 4 8
in this scenario there will be three plots of Y vs Z. how do I structure the loop around
bar(table.Y,table.Z)
thank you.

Accepted Answer

Star Strider
Star Strider on 19 May 2022
Try this —
CA = {'A' 1 3
'A' 2 4
'A' 3 10
'B' 1 4
'B' 2 4
'B' 4 6
'C' 0 1
'C' 1 4
'C' 2 5
'C' 3 7
'C' 4 8};
T1 = cell2table(CA, 'VariableNames',{'X','Y','Z'})
T1 = 11×3 table
X Y Z _____ _ __ {'A'} 1 3 {'A'} 2 4 {'A'} 3 10 {'B'} 1 4 {'B'} 2 4 {'B'} 4 6 {'C'} 0 1 {'C'} 1 4 {'C'} 2 5 {'C'} 3 7 {'C'} 4 8
[U1,~,ix] = unique(T1.X);
L = numel(U1);
figure
for k = 1:L
subplot(L,1,k)
v = ix == k;
plot(T1.Y(v), T1.Z(v), '.-')
grid
xlabel('Y')
ylabel('Z')
title(U1{k})
ylim([0 10])
end
There are ways to do this that may be more efficient for large arrays, although they require more coding as well.
.
  24 Comments
Frederick Awuah-Gyasi
Frederick Awuah-Gyasi on 23 May 2022
Thanks @Star Strider. I will try posting it as a question. Let's see if anyone comes to help. I will keep trying too. Now I'm getting the 3 animations except each animation should be exclusive to a group from column X
Star Strider
Star Strider on 23 May 2022
As always, my pleasure!
Perhaps someone else has some new insights that I do have not at this point.

Sign in to comment.

More Answers (1)

KSSV
KSSV on 19 May 2022
Let data be your three column matrix.
x = data(:,1) ;
y = data(:,2) ;
z = data(:,3) ;
xi = unique(x) ;
yi = unique(y) ;
nx = length(xi) ;
ny = length(yi) ;
% Matrices
[X,Y] = meshgrid(x,y) ;
Z = reshape(z,[ny,nx]) ;
  1 Comment
Frederick Awuah-Gyasi
Frederick Awuah-Gyasi on 19 May 2022
Thank you. I will try this out. But was hoping for example
For X in list (X) :
plot(Y,Z)
For if X = A
the data to plot will beb
Y Z
1 3
2 4
3 10

Sign in to comment.

Categories

Find more on Graphics Object Programming 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!