Generate animation(mp4) from for loop with plot

19 次查看(过去 30 天)
This code produces/generates 6 plots A1, A2, B1, B2, C1, C3. I am trying to produce 3 animations one for A1&A2, another for B1, B2 and a 3rd one for C1 & C3. Currently I have the 3 animations but in each one I see all 6 plots. Any help? My if conditions doesn't seem to work. Thank you.
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
thanks to @Star Strider help on getting this far.
  2 个评论
Johan
Johan 2022-5-23
I think the error comes from your Vector(k) statement, you don't reset it between each loop. Maybe this modifications would fix it ?
for b = 1:length(Names)
index = 1; %iniate a variable for Vector indexing
clear Vector %clear Vector to make sure you don't save leftover data in your movie
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
Thanks @Johan Pelloux-Prayer. getting closer I guess. got one animation with only A1, A2 (which is good) with an error
Error using VideoWriter/writeVideo (line 414)
The 'cdata' field of FRAME must not be empty
Error in test (line 53)
writeVideo(myWriter,Vector);

请先登录,再进行评论。

采纳的回答

Frederick Awuah-Gyasi
Fixed. Create a sub table for each set in column X and use the same code. Worked. Thanks all.
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));
%L = size(ID,1);
Names = unique(T1.X)
for b = 1:length(Names)
SetA = T1(contains(T1.X,Names(b)),:)
[ix,ID] = findgroups(SetA(:,1:2));
L = size(ID,1);
for k = 1:L
figure(k) %subplot(L,1,k)
v = find(ix == k);
plot(SetA.Y(v), SetA.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
moviename = strcat('Level_1_Movie_', Names(b))
myWriter = VideoWriter(moviename{1},'MPEG-4');
myWriter.FrameRate = 20;
open(myWriter);
writeVideo(myWriter,Vector);
close(myWriter);
clear Vector
end
  1 个评论
Frederick Awuah-Gyasi
@Star Strider can you check it out. Didn't want to celebrate alone. pls run it and see what our destination was. You did most of the work. Thank you.

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by