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 (한국어)