이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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
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
2022년 5월 19일
Thank you @Star Strider. Excatly what I was looking for. Instead of putting it all in one plot I separated it as there are 56 of them. God bless you.
Frederick Awuah-Gyasi
2022년 5월 20일
Hi @Star Strider I'm running the code now with two unique columns now. so i did this instead . the new column is B
[U1,~,ix] = unique(T(:,1:2),'rows');
but i'm getting this error
Subscripting into a table using one subscript (as in t(i)) is not supported. Specify a row subscript and a variable subscript, as in t(rows,vars). To
select variables, use t(:,i) or for one variable t.(i). To select rows, use t(i,:).
Frederick Awuah-Gyasi
2022년 5월 20일
in the meantime I will try concatenating the colums and use the old logic
Star Strider
2022년 5월 20일
I may need to know what ‘T’ is (at least some representative rows of it).
The purpose of the unique call was to get numeric indices representing the first column. Conccatenating the first colum (with the alphanumeric indicators) with a numeric column will not work. That may be throwing the error, since I get the same error when I use your unique call with the existing table.
The findgroups function will work with ‘X’ and ‘Y’ to give a unique index (although here there are no duplicates in these data).
Other parts of the code change as well —
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
[ix,ID] = findgroups(T1(:,1:2))
ix = 11×1
1
2
3
4
5
6
7
8
9
10
ID = 11×2 table
X Y
_____ _
{'A'} 1
{'A'} 2
{'A'} 3
{'B'} 1
{'B'} 2
{'B'} 4
{'C'} 0
{'C'} 1
{'C'} 2
{'C'} 3
{'C'} 4
L = numel(ix);
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
.
Frederick Awuah-Gyasi
2022년 5월 21일
Thanks so much. So I concanated the 1st and 2nd column. This is a verison of the first code. this second version was to plot cumulations so another coulmn was added. that hurlde seems to have been crossed a last piece is to animate the plots. So for each A, B C I will have animated plots.
[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])
saveas(gcf,name)
animeVector(k) = getframe;
end
myWriter = VideoWriter('LevelMovie');
myWriter.FrameRate = 20;
open(myWriter);
writeVideo(myWriter,animeVector);
close(myWriter);
I modified the code as above but i'm getting this error :
Error using VideoWriter/writeVideo (line 414)
All 'cdata' fields in FRAMES must be the same size.
Really appecaite all the help.
Frederick Awuah-Gyasi
2022년 5월 21일
Is there a comamand or parameter to pass so that 1, and 3 even though there are not data points still take the full space. like below. Not sure how both are not consistent.
Sorry to bother you with many questions. Thanks for the help.
Star Strider
2022년 5월 21일
I have no recent experience with videoWriter, however I will do my best to see if I can get this to work.
I am eliminating the saveas call for the time being, since it may not work with the online Run feature here.
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
[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])
% saveas(gcf,name)
GF = getframe(gcf) % Test
animeVector(k) = getframe(gcf);
end
GF = struct with fields:
cdata: [433×577×3 uint8]
colormap: []
GF = struct with fields:
cdata: [433×577×3 uint8]
colormap: []
GF = struct with fields:
cdata: [433×577×3 uint8]
colormap: []
myWriter = VideoWriter('LevelMovie');
myWriter.FrameRate = 20;
open(myWriter);
writeVideo(myWriter,animeVector);
close(myWriter);
Using:
animeVector(k) = getframe(gcf);
appears to run without error.
.
Star Strider
2022년 5월 21일
My apologies, however at this point I am completely lost.
I have no idea what you want to do.
Frederick Awuah-Gyasi
2022년 5월 21일
@Star Strider I have edited the commnent above. In response to your comment . Hope it makes sense now. sorry about the confusion.
Frederick Awuah-Gyasi
2022년 5월 21일
편집: Frederick Awuah-Gyasi
2022년 5월 21일
If my data is of this format with the added W column. How do I get the animation for A, B C. sepearately.
X W Y Z
A 1 0 3
A 1 2 3
A 1 3 3
A 2 0 5
A 2 2 7
A 2 3 8
B 1 0 3
B 1 1 3
B 1 3 3
B 2 0 12
B 2 2 1
B 2 3 3
C 1 0 2
C 1 1 1
C 1 2 3
C 2 0 4
C 2 1 3
C 2 2 6
I have edited the code to this to this :
[ix,ID] = findgroups(T1(:,1:2))
L = numel(U1);
figure
for k = 1:L
figure(k) %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])
GF = getframe(gcf) % Test
animeVector(k) = getframe(gcf);
end
myWriter = VideoWriter('LevelMovie');
myWriter.FrameRate = 20;
open(myWriter);
writeVideo(myWriter,animeVector);
close(myWriter);
Does it look right.
Star Strider
2022년 5월 22일
I have no idea if it looks right, however with a few changes in the code, it runs without error —
CA = {'A' 1 0 3
'A' 1 2 3
'A' 1 3 3
'A' 2 0 5
'A' 2 2 7
'A' 2 3 8
'B' 1 0 3
'B' 1 1 3
'B' 1 3 3
'B' 2 0 12
'B' 2 2 1
'B' 2 3 3
'C' 1 0 2
'C' 1 1 1
'C' 1 2 3
'C' 2 0 4
'C' 2 1 3
'C' 2 2 6};
T1 = cell2table(CA,'VariableNames',{'X','W','Y','Z'})
T1 = 18×4 table
X W Y Z
_____ _ _ __
{'A'} 1 0 3
{'A'} 1 2 3
{'A'} 1 3 3
{'A'} 2 0 5
{'A'} 2 2 7
{'A'} 2 3 8
{'B'} 1 0 3
{'B'} 1 1 3
{'B'} 1 3 3
{'B'} 2 0 12
{'B'} 2 2 1
{'B'} 2 3 3
{'C'} 1 0 2
{'C'} 1 1 1
{'C'} 1 2 3
{'C'} 2 0 4
[ix,ID] = findgroups(T1(:,1:2))
ix = 18×1
1
1
1
2
2
2
3
3
3
4
ID = 6×2 table
X W
_____ _
{'A'} 1
{'A'} 2
{'B'} 1
{'B'} 2
{'C'} 1
{'C'} 2
L = size(ID,1);
figure
for k = 1:L
figure(k) %subplot(L,1,k)
v = ix == k;
plot(T1.Y(v), T1.Z(v), '.-')
grid
xlabel('Y')
ylabel('Z')
title(sprintf('%s %d',ID{k,1}{:},ID{k,2}))
ylim([0 10])
GF = getframe(gcf) % Test
animeVector(k) = getframe(gcf);
end
GF = struct with fields:
cdata: [433×577×3 uint8]
colormap: []
GF = struct with fields:
cdata: [433×577×3 uint8]
colormap: []
GF = struct with fields:
cdata: [433×577×3 uint8]
colormap: []
GF = struct with fields:
cdata: [433×577×3 uint8]
colormap: []
GF = struct with fields:
cdata: [433×577×3 uint8]
colormap: []
GF = struct with fields:
cdata: [433×577×3 uint8]
colormap: []
myWriter = VideoWriter('LevelMovie');
myWriter.FrameRate = 20;
open(myWriter);
writeVideo(myWriter,animeVector);
close(myWriter);
I have no idea how this relates to your other post Cross product of two tables without common key so I will leave that alone.
.
Frederick Awuah-Gyasi
2022년 5월 22일
Thanks @Star Strider. Instead of A1 A2 B1 B2 C1 C2 for the animations. Will it be possible to have A, B C. So the animation shows A {A1 and A2 ) combined, B(B1 and B2) C (C1 and C2) . W is accually a day column shown ie. day 1 2 3 etc.
yeah that post is related but didnt want to confused the two. will explain that later. Z column must have data (0,1,2,3,4) there are instances where the are no data points so will have to fix that.
really appreciate all the help.
Star Strider
2022년 5월 22일
I have no idea how you want to combine them.
Try this —
CA = {'A' 1 0 3
'A' 1 2 3
'A' 1 3 3
'A' 2 0 5
'A' 2 2 7
'A' 2 3 8
'B' 1 0 3
'B' 1 1 3
'B' 1 3 3
'B' 2 0 12
'B' 2 2 1
'B' 2 3 3
'C' 1 0 2
'C' 1 1 1
'C' 1 2 3
'C' 2 0 4
'C' 2 1 3
'C' 2 2 6};
T1 = cell2table(CA,'VariableNames',{'X','W','Y','Z'})
T1 = 18×4 table
X W Y Z
_____ _ _ __
{'A'} 1 0 3
{'A'} 1 2 3
{'A'} 1 3 3
{'A'} 2 0 5
{'A'} 2 2 7
{'A'} 2 3 8
{'B'} 1 0 3
{'B'} 1 1 3
{'B'} 1 3 3
{'B'} 2 0 12
{'B'} 2 2 1
{'B'} 2 3 3
{'C'} 1 0 2
{'C'} 1 1 1
{'C'} 1 2 3
{'C'} 2 0 4
[ix,ID] = findgroups(T1(:,1:2))
ix = 18×1
1
1
1
2
2
2
3
3
3
4
ID = 6×2 table
X W
_____ _
{'A'} 1
{'A'} 2
{'B'} 1
{'B'} 2
{'C'} 1
{'C'} 2
L = size(ID,1);
figure
for k = 1:2:L
figure(k) %subplot(L,1,k)
v = find(ix == k);
v2 = v:v+5;
plot(T1.Y(v2(1:3)), T1.Z(v2(1:3)), '.-')
hold on
plot(T1.Y(v2(1:3)+3), T1.Z(v2(1:3)+3), '.-')
hold off
grid
xlabel('Y')
ylabel('Z')
title(sprintf('%s',ID{k,1}{:}))
ylim([0 10])
GF = getframe(gcf) % Test
animeVector(k) = getframe(gcf);
end
v = 3×1
1
2
3
GF = struct with fields:
cdata: [433×577×3 uint8]
colormap: []
v = 3×1
7
8
9
GF = struct with fields:
cdata: [433×577×3 uint8]
colormap: []
v = 3×1
13
14
15
GF = struct with fields:
cdata: [433×577×3 uint8]
colormap: []
myWriter = VideoWriter('LevelMovie');
myWriter.FrameRate = 20;
open(myWriter);
writeVideo(myWriter,animeVector);
Error using VideoWriter/writeVideo
The 'cdata' field of FRAME must not be empty
The 'cdata' field of FRAME must not be empty
close(myWriter);
This combines the plots.
I defer to you to solve the writeVideo error.
.
Frederick Awuah-Gyasi
2022년 5월 22일
편집: Frederick Awuah-Gyasi
2022년 5월 22일
@Star Strider: what I want to be able to do is loop through and have A1 and A2 producing one animation,B1,B2 producing a second animation and C1 C2 producing a 3rd animation.
this code
for k = 1:L
figure(k) %subplot(L,1,k)
v = find(ix == k);
plot(T1.Y(v), T1.Z(v), '.-')
grid
xlabel('Y')
ylabel('Z')
title(sprintf('%s %d',ID{k,1}{:},ID{k,2}))
ylim([0 10])
%GF = getframe(gcf) % Test
%Vector(k) = getframe(gcf);
end
produces A1, A2, B1, B2, C1, C2. I think my use of the word combine was wrong. wanted A1, A2 producing an animation separeate from B1, B2. Hope this makes sense. Sorry for bothering you so long.
Star Strider
2022년 5월 23일
That is what my earlier code did.
Please experiment with the code I already wrote to get the result you want.
I am lost and obviously cannot figure this out.
Frederick Awuah-Gyasi
2022년 5월 23일
@Star Strider so sorry I lost you. I'm back to the earlier code and will keep you posted tonight. I really do appreciate all the help.
Star Strider
2022년 5월 23일
As always, my pleasure!
I just do not know where else to go with this, since we seem to be in an infinite loop.
Frederick Awuah-Gyasi
2022년 5월 23일
@Star Strider this code produces 3 .mp4 files which I want. but Level_1_Movie_A.mp4 for instanfce should be just for A(show only A1 and A2) same for B and C. somehow the if condition is not working. Any ideas?
CA = {'A' 1 0 3
'A' 1 2 3
'A' 1 3 3
'A' 2 0 5
'A' 2 2 7
'A' 2 3 8
'B' 1 0 3
'B' 1 1 3
'B' 1 3 3
'B' 2 0 12
'B' 2 2 1
'B' 2 3 3
'C' 1 0 2
'C' 1 1 1
'C' 1 2 3
'C' 2 0 4
'C' 2 1 3
'C' 2 2 6};
T1 = cell2table(CA,'VariableNames',{'X','W','Y','Z'});
[ix,ID] = findgroups(T1(:,1:2));
[U1,~,ixx] = unique(T1.X);
L = size(ID,1);
Names = unique(T1.X)
M = numel(U1);
for b = 1:length(Names)
for k = 1:L
figure(k) %subplot(L,1,k)
%
v = find(ix == k);
data=unique(T1.X(v))
Names(b)
ismember( Names(b), data )
if ismember( Names(b), data )
T1.X(v)
plot(T1.Y(v), T1.Z(v), '.-')
grid
xlabel('Y')
ylabel('Z')
title(sprintf('%s %d',ID{k,1}{:},ID{k,2}))
ylim([0 10])
GF = getframe(gcf) % Test
Vector(k) = getframe(gcf);
end
end
moviename = strcat('Level_1_Movie_', Names(b))
myWriter = VideoWriter(moviename{1},'MPEG-4');
myWriter.FrameRate = 20;
open(myWriter);
writeVideo(myWriter,Vector);
close(myWriter);
end
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
2022년 5월 23일
As always, my pleasure!
Perhaps someone else has some new insights that I do have not at this point.
추가 답변 (1개)
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
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 Center 및 File Exchange에서 Geographic Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)