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

标签 matlab animation graph model plot

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

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

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

谢谢 卢克

最佳答案

这是 @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

enter image description here

关于matlab - 在 MATLAB 中创建沿图形移动的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4959528/

相关文章:

javascript - 使用 Javascript 进行逐帧动画

c++ - 深度优先图遍历

c++ - 多模态图像对齐问题

c - 辨别值 255 是真实数据还是错误输出

matlab - 获取在 Matlab 代码中执行的实际行

c++ - boost 图 : How to copy the nodes and edges of a graph without copying properties?

C、检测有向图中的循环、DFS、不一致图

matlab - 如何在调用mapreduce函数时将参数传递给map函数?

jquery - 为什么我的 CSS3/jQuery 动画的性能这么慢?

c++ - QPropertyAnimation 没有动画