Iterating through alternating for loops
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
mickeymouse
el 16 de Jun. de 2022
Comentada: Voss
el 20 de Jun. de 2022
I am trying to run a code where I want to plot strides side by side and alternate between plotting one left and one right stride. To do this I am running 2 for loops where one is nested in another. What I was hoping to achieve is where one loop is run and then the other one and this pattern alternates until all steps are plotted (left,right,left,right...). What is currently happening is that one loop run and then all of the second for loop runs without switching back to the previous one. I would like some help in understanding what I am doing wrong here.
For context I have put a portion of my code below:
t = tiledlayout(1,2);
nexttile;
hold on;
for(ii=1:size(right_strides.ltrl,2))
plot(right_strides.ltrl(:,ii),right_strides.frwd(:,ii),'Color','b')
x = right_strides.ltrl(end,ii);
y = right_strides.frwd(end,ii);
h = animatedline;
for i =1:length(x)
addpoints(h,x(i),y(i));
drawnow;
end;
nexttile;
hold on;
for(ii=1:size(left_strides.ltrl,2))
plot(left_strides.ltrl(:,ii),left_strides.frwd(:,ii),'Color','r')
xL = left_strides.ltrl(end,ii);
yL = left_strides.frwd(end,ii);
hL = animatedline;
for i =1:length(x)
addpoints(hL,xL(i),yL(i));
drawnow;
end
end;
end;
Any help is appreciated!
0 comentarios
Respuesta aceptada
Voss
el 16 de Jun. de 2022
It seems like you need only one for loop, where in each iteration you add a single left stride and a single right stride.
Something like this:
% I make up some data that I think has the same structure as yours:
left_strides = struct('ltrl',randn(1,100),'frwd',sort(rand(1,100)));
right_strides = struct('ltrl',randn(1,100),'frwd',sort(rand(1,100)));
figure();
t = tiledlayout(1,2);
% create the lines before the loop:
left_ax = nexttile;
left_line = animatedline('Color','r','Marker','.');
right_ax = nexttile;
right_line = animatedline('Color','b','Marker','.');
% linkaxes([left_ax right_ax],'y'); % maybe keep the y-lim the same between the two axes
% number of iterations is minimum of the number of left strides and the
% number of right strides (I imagine they are the same length, but just in
% case ...):
N_iter = min(numel(left_strides.ltrl),numel(right_strides.ltrl));
% for each iteration of the loop, add a point to the left line and a point
% to the right line
for ii = 1:N_iter
addpoints(left_line,left_strides.ltrl(ii),left_strides.frwd(ii));
drawnow;
% pause(0.25); % maybe include a pause so you can see the strides happening
addpoints(right_line,right_strides.ltrl(ii),right_strides.frwd(ii));
drawnow;
% pause(0.25);
end
2 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Animation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
