创建点沿MATLAB图形移动 [英] Creating a point moving along a graph in MATLAB
问题描述
我期待建立一个简单的日志(X)的内MATLAB图,其中模型显示该点沿曲线移动的随时间。
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!
谢谢
卢克
推荐答案
下面是关于<一个变化href=\"http://stackoverflow.com/questions/4959528/creating-a-point-moving-along-a-graph-in-matlab/4959858#4959858\">@Jacob's解。相反,在每帧( 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屋!