Generate animation(mp4) from for loop with plot

19 visualizzazioni (ultimi 30 giorni)
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 Commenti
Johan
Johan il 23 Mag 2022
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 il 23 Mag 2022
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);

Accedi per commentare.

Risposta accettata

Frederick Awuah-Gyasi
Frederick Awuah-Gyasi il 23 Mag 2022
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 Commento
Frederick Awuah-Gyasi
Frederick Awuah-Gyasi il 23 Mag 2022
@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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Animation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by