Generate animation(mp4) from for loop with plot

조회 수: 19 (최근 30일)
Frederick Awuah-Gyasi
Frederick Awuah-Gyasi 2022년 5월 23일
댓글: Frederick Awuah-Gyasi 2022년 5월 23일
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
Frederick Awuah-Gyasi 2022년 5월 23일
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
Frederick Awuah-Gyasi 2022년 5월 23일
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
Frederick Awuah-Gyasi 2022년 5월 23일
@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