在MATLAB中创建沿图形移动的点

Luk*_*eyB 3 matlab plot animation model graph

我期待在MATLAB中创建一个简单的log(x)图,其中模型显示随时间沿曲线移动的点.

总体目标是将这些图中的两个并排放置并对其应用算法.我真的不确定从哪里开始.

我对MATLAB编码比较陌生,所以任何帮助都会非常有用!

谢谢卢克

Amr*_*mro 7

以下是@Jacob解决方案的变体.clf我们只需更新点的位置,而不是重绘每一帧()的所有内容:

%# control animation speed
DELAY = 0.01;
numPoints = 600;

%# create data
x = linspace(0,10,numPoints);
y = log(x);

%# plot graph
figure('DoubleBuffer','on')                  %# no flickering
plot(x,y, 'LineWidth',2), grid on
xlabel('x'), ylabel('y'), title('y = log(x)')

%# create moving point + coords text
hLine = line('XData',x(1), 'YData',y(1), 'Color','r', ...
    'Marker','o', 'MarkerSize',6, 'LineWidth',2);
hTxt = text(x(1), y(1), sprintf('(%.3f,%.3f)',x(1),y(1)), ...
    'Color',[0.2 0.2 0.2], 'FontSize',8, ...
    'HorizontalAlignment','left', 'VerticalAlignment','top');

%# infinite loop
i = 1;                                       %# index
while true      
    %# update point & text
    set(hLine, 'XData',x(i), 'YData',y(i))   
    set(hTxt, 'Position',[x(i) y(i)], ...
        'String',sprintf('(%.3f,%.3f)',[x(i) y(i)]))        
    drawnow                                  %# force refresh
    %#pause(DELAY)                           %# slow down animation

    i = rem(i+1,numPoints)+1;                %# circular increment
    if ~ishandle(hLine), break; end          %# in case you close the figure
end
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述