在 MATLAB 中创建沿图形移动的点 [英] Creating a point moving along a graph in MATLAB

查看:56
本文介绍了在 MATLAB 中创建沿图形移动的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking to create a simple log(x) graph within MATLAB in which the model shows the point moving along the curve with time.

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

The overall aim is to have two of these graphs alongside one another and to apply an algorithm to them. I am really unsure where to start here.

我在 MATLAB 编码方面相对较新,所以任何帮助都会非常有用!

I am relatively new at MATLAB coding so any help would be very useful!

谢谢卢克

推荐答案

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

Here is a variation on @Jacob's solution. Instead of redrawing everything at each frame (clf) we simply update the point's location:

%# 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

这篇关于在 MATLAB 中创建沿图形移动的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆