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

4 views (last 30 days)
Frederick Awuah-Gyasi on 19 May 2022
Commented: Star Strider on 23 May 2022
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.
##### 2 CommentsShowHide 1 older comment
Star Strider on 19 May 2022

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.
.
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.

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]) ;
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