Automatic Animation Playback Outside Livescripts
조회 수: 28 (최근 30일)
이전 댓글 표시
I've been doing a lot of work with animations in livescripts, and I like that matlab seems to recognize whenever I'm animating and automatically adds a playback bar. I moved to a different project where using livescripts doesn't make as much sense but where I could still benefit from this feature. Short of spending a couple hours to make the same thing myself (but worse), is there a way to get this animation playback in a normal figure or uifigure?
답변 (1개)
Divyajyoti Nayak
2024년 11월 19일 3:05
편집: Divyajyoti Nayak
2024년 11월 19일 6:56
There's no direct way to include playback features for animated plots but I did manage to create a simple workaround by extracting each individual frame of the animation by using the 'getframe' function. To replay the animation, the 'movie' function can be used. Here's some documentation for these functions:
To incorporate playback features i used a slider type 'uicontrol' object and coded the functionality of playing the animation forward and backward in its callback. Here's the code:
%Sample Animation
h = animatedline;
axis([0,4*pi,-1,1])
x = linspace(0,4*pi,1000);
y = sin(x);
for k = 1:length(x)
addpoints(h,x(k),y(k));
drawnow
F(k) = getframe; %Store every frame
end
f2 = figure(2);
imshow(frame2im(F(1))); %Initiallize GUI with first frame
playbackSpeed = 2; %Variable to control playback speed
%The 'Max' and 'SliderStep' values will be different for different number
%of frames, do change them according to needs
slider = uicontrol('Style','slider','Min',0,'Max',1000,'SliderStep',[playbackSpeed*0.001,0.05],'Value',1);
slider.Callback = {@sliderCallback,F};
slider.Units = 'normalized';
slider.Position = [0.05 0.05 0.9 0.1];
function sliderCallback(src, ~, frames)
ax = src.Parent; %Getting handle of parent figure
src.Value = round(src.Value); %Rounding off slider value to nearest integer
if src.Value > 0
%Replacing the image color data with frame based on slider value
ax.Children(2).Children.CData = frame2im(frames(src.Value));
end
end
To play/rewind the animation click and hold the right/left arrow buttons of the slider. The slider can be used to get to particular frame. To increase the speed of the playback, the 'playbackSpeed' variable can be increased. Here's some documention of the properties of the slider 'uicontrol' object used in the code:
Hope this helps!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Animation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!