关闭图 window 会导致打开新图 window

Closing the figure window causes a new figure window to open

这是一个模拟时钟。它运行完美。但是一旦我关闭时钟,另一个数字就会打开,只有时钟指针显示。这使得停止程序变得困难。我怎样才能停止程序?

x=0;y=0;r=10;
hold on;
theta = 0:pi/60:2*pi;
xc = r * cos(theta);
yc = r * sin(theta);
h = plot(xc,yc,'r','linewidth',4);
axis off
r=9; i=1;
set(gca,'FontWeight','bold');

for theta = pi/6: pi/6: 2*pi
    y1 = r * cos(theta);
    x1 = r * sin(theta);
    plot([x1/9*8 x1/9*7],[y1/9*8 y1/9*7],'color',[0 0 1])
    text(x1/9*9.5,y1/9*9.5,num2str(i),'color',[0 0 1]);
    i=i+1;
end

for theta=pi/30 : pi/30 : 2*pi
    y1 = 10 * cos(theta);
    x1 = 10 * sin(theta);
    plot([x1/9*8 x1/9*7],[y1/9*8 y1/9*7],'color',[0 0 0])
end

while(1)
    tic
    c = clock;
    c = c(1,4:6);
    minute =c(1,2); sec=c(1,3);
    if (c(1,1)>12)
       hr = c(1,1)-12;
    else
       hr = c(1,1);
    end
    min1 = ceil(minute/12);
    theta = (hr*pi)/6 + (min1*pi)/30;
    f=figure(1); hold on;
    y1 = 3 * cos(theta); Yhr = [0 y1];
    x1 = 3 * sin(theta); Xhr = [0 x1];
    hrhnd=plot(Xhr,Yhr);hold on;
    theta1 = (minute*pi)/30;
    y2 = 4.5 * cos(theta1); Ymin = [0 y2];
    x2 = 4.5 * sin(theta1); Xmin = [0 x2];
    minhnd=plot(Xmin,Ymin);
    theta2 = (sec*pi)/30;
    y3 = 5 * cos(theta2); Ysec = [0 y3];
    x3 = 5 * sin(theta2); Xsec = [0 x3];
    sechnd=plot(Xsec,Ysec);
    z=toc;
    pause(1-z);
     delete(sechnd);
     delete(minhnd);
     delete(hrhnd);
end

关闭 window 时停止脚本的最简单方法是在 window 仍然存在的情况下在其循环内测试脚本。

我们通过创建图形 window 并记录其句柄来启动脚本:

fig = figure;

接下来,在循环中,我们使用 ishandle:

检查 window 是否仍然存在
while(ishandle(fig))
   ...
end

完整节目:

x=0;y=0;r=10;
fig = figure;       %!!! NEW LINE
hold on;
theta = 0:pi/60:2*pi;
xc = r * cos(theta);
yc = r * sin(theta);
h = plot(xc,yc,'r','linewidth',4);
axis off
r=9; i=1;
set(gca,'FontWeight','bold');

for theta = pi/6: pi/6: 2*pi
    y1 = r * cos(theta);
    x1 = r * sin(theta);
    plot([x1/9*8 x1/9*7],[y1/9*8 y1/9*7],'color',[0 0 1])
    text(x1/9*9.5,y1/9*9.5,num2str(i),'color',[0 0 1]);
    i=i+1;
end

for theta=pi/30 : pi/30 : 2*pi
    y1 = 10 * cos(theta);
    x1 = 10 * sin(theta);
    plot([x1/9*8 x1/9*7],[y1/9*8 y1/9*7],'color',[0 0 0])
end

while(ishandle(fig))    %!!! UPDATED LINE
    tic
    c = clock;
    c = c(1,4:6);
    minute =c(1,2); sec=c(1,3);
    if (c(1,1)>12)
       hr = c(1,1)-12;
    else
       hr = c(1,1);
    end
    min1 = ceil(minute/12);
    theta = (hr*pi)/6 + (min1*pi)/30;
    f=figure(1); hold on;
    y1 = 3 * cos(theta); Yhr = [0 y1];
    x1 = 3 * sin(theta); Xhr = [0 x1];
    hrhnd=plot(Xhr,Yhr);hold on;
    theta1 = (minute*pi)/30;
    y2 = 4.5 * cos(theta1); Ymin = [0 y2];
    x2 = 4.5 * sin(theta1); Xmin = [0 x2];
    minhnd=plot(Xmin,Ymin);
    theta2 = (sec*pi)/30;
    y3 = 5 * cos(theta2); Ysec = [0 y3];
    x3 = 5 * sin(theta2); Xsec = [0 x3];
    sechnd=plot(Xsec,Ysec);
    z=toc;
    pause(1-z);
     delete(sechnd);
     delete(minhnd);
     delete(hrhnd);
end

您可以通过不删除并重新绘制手,而是更新它们的位置来改进您的程序。你会在循环之前做 hrhnd=plot(Xhr,Yhr);,在它的初始位置画手,然后 set(hrhnd,'XData',Xhr,'YData', Yhr) 更新它的位置。

你也可以在画好钟面后axis equal,确保它是圆的。

请注意,您只需要在顶部给出一次 hold on,而不是在每个 plot 命令之后都需要它。