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
Frederick Awuah-Gyasi 2022년 5월 23일
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
Frederick Awuah-Gyasi 2022년 5월 19일
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