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

编程入门 行业动态 更新时间:2024-10-10 19:26:44
本文介绍了在 MATLAB 中创建沿图形移动的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我希望在 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-01 09:33:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/826330.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图形   MATLAB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!