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

1 次查看(过去 30 天)
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.

采纳的回答

Star Strider
Star Strider 2022-5-19
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 个评论
Frederick Awuah-Gyasi
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 2022-5-23
As always, my pleasure!
Perhaps someone else has some new insights that I do have not at this point.

请先登录,再进行评论。

更多回答(1 个)

KSSV
KSSV 2022-5-19
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 个评论
Frederick Awuah-Gyasi
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Geographic Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by