尽管在整个过程中使用相同的图形句柄,但在 matlab 图形中创建的多边形副本

Copy of polygon created in matlab figure despite using same figure handle throughout

    figure;
    poly = fill(sq(:,1), sq(:,2), 'b');
    %% create cell "p" consisting of 5600 arrays of same size as sq
    for i=1:5600
        hold on
        set(poly, 'X', p{i}(:,1),...
            'Y', p{i}(:,2));
        hold off
        drawnow;
    end
    %% again create cell "q" consisting of 8700 arrays of same size as sq
    for i=1:8700
        hold on
        set(poly, 'X', q{i}(:,1),...
            'Y', q{i}(:,2));
        hold off
        drawnow;
    end

我在第一行创建了一个蓝色填充的多边形,然后将其移动到整个图形。当我 运行 上述代码时,第一部分将受 p 控制的多边形从初始点 x0 移动到 x1。然后我在代码的第二部分制作另一个单元格 q 并使用它将蓝色填充的多边形再次从 x1 移动到 x2。但是这次多边形的副本是在 x1 处创建的,它会移动,因此当这个新多边形移动到 x2 时,先前的多边形仍在 x1 处。为什么会这样?

我尝试使用一些不同的代码(更高效)来编写您描述的内容,并弥补了您未添加的部分。它正在运行,所以如果这正是您要查找的内容,您可以采用此代码或将其与您的代码进行比较并查找问题。

我的代码:

% define some parameters and auxilary function:
sq_size = 3;
makeSq = @(xy) [xy(1) xy(2)
    xy(1)+sq_size xy(2)
    xy(1)+sq_size xy(2)+sq_size
    xy(1) xy(2)+sq_size];

% create the first object:
sq = makeSq([1 1]);
poly = fill(sq(:,1), sq(:,2), 'b');

% setting the limmits for a constant view:
xlim([0 50+sq_size])
ylim([0 50+sq_size])

% first loop:
p = randi(50,10,2); % no need for cell here...
for k = 1:size(p,1)
    temp_sq = makeSq(p(k,:));
    set(poly, 'X', temp_sq(:,1),'Y',temp_sq(:,2));
    drawnow;
    pause(0.1)
end

% second loop:
q = randi(50,20,2); % no need for cell here...
set(poly,'FaceColor','g') % change to green to see we are in the second loop
for k = 1:size(q,1)
    temp_sq = makeSq(q(k,:));
    set(poly,'X',temp_sq(:,1),'Y',temp_sq(:,2));
    drawnow;
    pause(0.1)
end

pause只是为了看动画,并不是真正需要的。