KKS
KKS

Reputation: 1389

How to stop matlab figure pop up

for m=1:10;
    pause(0.2)
    h1=figure(1);
    set(h1,'Position',[200 200 600 500]);
    pause(0.2)
    h2=figure(2);
    set(h2,'Position',[600 200 600 500]);
end

When I run upper code, Two figures pop up alternating with each other like below.

enter image description here

enter image description here

I would like to stop this pop up property and keep the position during whole iteration like below.

enter image description here

How could I change this property?

Thank you in advance.

I add a code. in this code, two figures are continuously blinking. What I want to do is stopping this blinking.

clear all
close all
clc
%%patch1%%
x1=[-2 0 0 -2];
y1=[-1 -1 1 1];
z1=[0 0 0 0];
a=patch(x1,y1,z1,'green');
c=patch(x1,y1,z1,'green');

%%patch2%%
x2=[0 2 2 0];
y2=[-1 -1 1 1];
z2=[0 0 0 0];
b=patch(x2,y2,z2,'red');
d=patch(x2,y2,z2,'red');

%h1=figure(1),grid on

    for k=0:pi/10:10*pi
        delete([a b])
        figure(1),grid on
        a=patch(x1,y1,z1+[-0.1*sin(k) 0.3*sin(k) 0.3*sin(k) -0.1*sin(k)],'green');
        b=patch(x2,y2,z2+[0.3*sin(k) -0.1*sin(k) -0.1*sin(k) 0.3*sin(k)],'red');
        axis([-2.5 2.5 -1.5 1.5 -0.5 0.5])

        pause(0.05)
        delete([c d])
        figure(2),grid on
        c=patch(x1,y1,z1+[-0.3*sin(k) 0.05*sin(k) -0.05*sin(k) -0.4*sin(k)],'green');
        d=patch(x2,y2,z2+[0.05*sin(k) 0.4*sin(k) 0.3*sin(k) -0.05*sin(k)],'red');
        axis([-2.5 2.5 -1.5 1.5 -0.5 0.5])

        pause(0.05)

    end

Upvotes: 1

Views: 1731

Answers (3)

NeitherNor
NeitherNor

Reputation: 266

Instead of adding and removing each patch in each iteration, consider first creating them outside the loop, and then to just change them inside the loop:

%%patch1%%
x1=[-2 0 0 -2];
y1=[-1 -1 1 1];
z1=[0 0 0 0];

%%patch2%%
x2=[0 2 2 0];
y2=[-1 -1 1 1];
z2=[0 0 0 0];

figure(1),grid on
a=patch(x1,y1,z1,'green');
b=patch(x2,y2,z2,'red');
axis([-2.5 2.5 -1.5 1.5 -0.5 0.5])

figure(2),grid on
c=patch(x1,y1,z1,'green');
d=patch(x2,y2,z2,'red');
axis([-2.5 2.5 -1.5 1.5 -0.5 0.5])

for k=0:pi/10:10*pi
    set(a, 'ZData', z1+[-0.1*sin(k) 0.3*sin(k) 0.3*sin(k) -0.1*sin(k)]);
    set(b, 'ZData',z2+[0.3*sin(k) -0.1*sin(k) -0.1*sin(k) 0.3*sin(k)]);

    pause(0.05)
    set(c, 'ZData', z1+[-0.3*sin(k) 0.05*sin(k) -0.05*sin(k) -0.4*sin(k)]);
    set(d, 'ZData', z2+[0.05*sin(k) 0.4*sin(k) 0.3*sin(k) -0.05*sin(k)]);

    pause(0.05)
end

Upvotes: 1

sco1
sco1

Reputation: 12214

Use the ''Parent'' property of the patch object to specify the axes to plot on without needing the figure call to update the current axes.

Using your example:

f(1) = figure;
f(2) = figure;
ax(1) = axes('Parent', f(1));
ax(2) = axes('Parent', f(2));

for ii = 1:length(ax)
    axis(ax(ii), [-2.5 2.5 -1.5 1.5 -0.5 0.5]);
    grid(ax(ii), 'on');
    hold(ax(ii), 'on');
end

%%patch1%%
x1=[-2 0 0 -2];
y1=[-1 -1 1 1];
z1=[0 0 0 0];
a=patch(x1,y1,z1,'green', 'Parent', ax(1));
c=patch(x1,y1,z1,'green', 'Parent', ax(2));

%%patch2%%
x2=[0 2 2 0];
y2=[-1 -1 1 1];
z2=[0 0 0 0];
b=patch(x2,y2,z2,'red', 'Parent', ax(1));
d=patch(x2,y2,z2,'red', 'Parent', ax(2));

for k=0:pi/10:10*pi
    delete([a b])
    a=patch(x1,y1,z1+[-0.1*sin(k) 0.3*sin(k) 0.3*sin(k) -0.1*sin(k)],'green', 'Parent', ax(1));
    b=patch(x2,y2,z2+[0.3*sin(k) -0.1*sin(k) -0.1*sin(k) 0.3*sin(k)],'red', 'Parent', ax(1));

    pause(0.05)
    delete([c d])
    c=patch(x1,y1,z1+[-0.3*sin(k) 0.05*sin(k) -0.05*sin(k) -0.4*sin(k)],'green', 'Parent', ax(2));
    d=patch(x2,y2,z2+[0.05*sin(k) 0.4*sin(k) 0.3*sin(k) -0.05*sin(k)],'red', 'Parent', ax(2));
    pause(0.05)
end

for ii = 1:length(ax)
    hold(ax(ii), 'off');
end

As an aside, you can also update your patches by modifying the 'XData', 'YData', and 'ZData' of the patch objects rather than deleting & creating patch objects with every loop. This is generally more efficient.

Using your primary for loop as an example (assumes R2014b or newer):

for k=0:pi/10:10*pi
    a.ZData = z1+[-0.1*sin(k) 0.3*sin(k) 0.3*sin(k) -0.1*sin(k)];
    b.ZData = z2+[0.3*sin(k) -0.1*sin(k) -0.1*sin(k) 0.3*sin(k)];
    pause(0.05)

    c.ZData = z1+[-0.3*sin(k) 0.05*sin(k) -0.05*sin(k) -0.4*sin(k)];
    d.ZData = z2+[0.05*sin(k) 0.4*sin(k) 0.3*sin(k) -0.05*sin(k)];
    pause(0.05)
end

Upvotes: 2

Jens Boldsen
Jens Boldsen

Reputation: 1275

You should avoid calling figure in a loop! It's the lines h1=figure(1); and h2=figure(2); that keeps switching the focus. Whenever you want to put something in the figure window, use the 'parent',h1 option pair or 'parent',ax1 for some axes object ax1. to avoid calling figure. I suggest that you only use figure to create the figure-window.

For example instead of using figure(1);image(img); start by creating the figure and axes objects (h1=figure(1);ax1=axes('Parent',h1);), and then whenever you want to update the image in the figure, use image(img,'Parent',ax1);.

Upvotes: 3

Related Questions