Matlab-更新绘图上的对象 [英] Matlab - updating objects on a plot
问题描述
我正在尝试为我正在研究的仿真添加一个计时器.目前,我可以让计时器显示在我想要的位置,但是我无法使数字彼此清楚,即它们都彼此堆叠在一起,慢慢地形成了坚实的黑色混乱.我已经尝试实现clf函数,但是它只是清除了整个图形.计时器的代码为:
I am trying to add a timer a simulation i am working on. At the moment i can get the timer to display in the location i want, however i cant make the numbers clear each other, i.e they all just stack on top of each other slowly creating a solid black mess. I have tried implementing a clf function but it just clears the entire figure. The code for the timer is:
HH = 0; MM = 0; SS = 0;
timer = sprintf('%02d:%02d:%02d',HH,MM,SS);
text(-450,450,timer); %% adjust location of clock in graph using the first two arguments: (x,y) coordinates
for t = 1:86400
SS = SS + 1;
if SS == 60
MM = MM + 1;
SS = 0;
end
if MM == 60
HH = HH + 1;
MM = 0;
end
timer = sprintf('%02d:%02d:%02d',HH,MM,SS); %% construct time string after all adjustments to HH, MM, SS
clf(f,'reset'); %% clear previous clock display
text(-450,450,timer); %% re-plot time to figure
if t == EventTimes(1)
uav1 = uav1.uavSetDestin([event1(2:3) 0]);
plot(event1(2),event1(3),'+')
hold on
end
if t == EventTimes(2)
uav2 = uav2.uavSetDestin([event2(2:3) 0]);
plot(event2(2),event2(3),'r+')
hold on
end
有没有办法我可以仅重置计时器功能,以便其正确显示?
Is there a way i can reset only the timer function so it displays properly?
推荐答案
您要存储处理文本对象并更新
You want to store the handle to the text object and update the String
property of this existing object rather than creating a new text
object every time.
%// The first time through your loop
htext = text(-450, 450, timer);
%// Every other time through the loop
set(htext, 'String', sprintf('%02d:%02d:%02d',HH,MM,SS)
您还希望对plot
对象执行类似的操作,而不是清除图形并在每次迭代时重新绘制所有图.
You will also want to do something similar with the plot
objects rather than clearing the figure and redrawing all of the plots every iteration.
将此与您的代码集成在一起,我们会得到类似的东西:
Integrating this with your code we get something like:
%// Create the initial text object
HH = 0; MM = 0; SS = 0;
timerString = sprintf('%02d:%02d:%02d',HH,MM,SS);
htext = text(-450, 450, timeString);
%// Create the plot objects
hplot1 = plot(NaN, NaN, '+');
hplot2 = plot(NaN, NaN, 'r+');
for t = 1:86400
SS = SS + 1;
%// I could help myself and made this significantly shorter
MM = MM + (mod(SS, 60) == 0);
HH = HH + (mod(MM, 60) == 0);
%// Update the timer string
timerString = sprintf('%02d:%02d:%02d',HH,MM,SS);
set(htext, 'String', timerString);
%// Update your plots depending upon which EventTimes()
if t == EventTimes(1)
uav1 = uav1.uavSetDestin([event1(2:3) 0]);
set(hplot1, 'XData', event1(2), 'YData', event1(3));
elseif t == EventTimes(2)
uav2 = uav2.uavSetDestin([event2(2:3) 0]);
set(hplot2, 'XData', event2(2), 'YData', event2(3));
end
%// Force a redraw event
drawnow;
end
这篇关于Matlab-更新绘图上的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!