Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Plot to a specific figure

Subject: Plot to a specific figure

From: Jeff

Date: 10 Jul, 2013 18:16:07

Message: 1 of 6

I activate and get the handle to a specific figure by

h=figure(2);

and plot to it using

for tt = 0:1:T
    plot(U(tt+1,:),'o'); axis(xs);
    title(sprintf([txt ', t=%d (of %d)'], tt, T), ...
        'fontweight','b','FontSize',12);
    xlabel(strXlbl,'fontweight','b');
    ylabel(strYlbl,'fontweight','b');
    drawnow;
end

But if I click on figure 1, Matlab starts plotting to that figure and I lose what's there. How do I force plotting to a figure even if user clicks on another? It's not in documentation for plot or figure command.

Subject: Plot to a specific figure

From: dpb

Date: 10 Jul, 2013 18:32:14

Message: 2 of 6

On 7/10/2013 1:16 PM, Jeff wrote:
> I activate and get the handle to a specific figure by
>
> h=figure(2);
>
> and plot to it using
>
> for tt = 0:1:T
> plot(U(tt+1,:),'o'); axis(xs);
> title(sprintf([txt ', t=%d (of %d)'], tt, T), ...
> 'fontweight','b','FontSize',12);
> xlabel(strXlbl,'fontweight','b');
> ylabel(strYlbl,'fontweight','b');
> drawnow;
> end
>
> But if I click on figure 1, Matlab starts plotting to that figure and I
> lose what's there. How do I force plotting to a figure even if user
> clicks on another? It's not in documentation for plot or figure command.

Au contraire... :)

 >> help figure
  figure Create figure window.
     figure, by itself, creates a new figure window, and returns
     its handle.

     figure(H) makes H the current figure, forces it to become visible,
     and raises it above all other figures on the screen. If Figure H
     does not exist, and H is an integer, a new figure is created with
     handle H.
     ...

Once you've got the handle, use it as the second form to make it the
current figure. To be effective if you have more than one that you want
to use at a time then you need the handle variable to be a vector of
handles and you must select the correct one for the desired target.

You can retrieve the current figure w/ gcf and compare to the desired;
if not the same execute the figure(h(index_desired)) before the plotting
commences in the function. Or if the figure is known to be a specific
one for the function, just hard code it in--if it exists it will make it
current; if it doesn't it'll create a new one and make _it_ current...


--

Subject: Plot to a specific figure

From: Steven_Lord

Date: 10 Jul, 2013 20:27:55

Message: 3 of 6



"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
news:krk8d7$79h$1@newscl01ah.mathworks.com...
> I activate and get the handle to a specific figure by
>
> h=figure(2);
>
> and plot to it using
>
> for tt = 0:1:T
> plot(U(tt+1,:),'o'); axis(xs);
> title(sprintf([txt ', t=%d (of %d)'], tt, T), ...
> 'fontweight','b','FontSize',12);
> xlabel(strXlbl,'fontweight','b');
> ylabel(strYlbl,'fontweight','b');
> drawnow;
> end
>
> But if I click on figure 1, Matlab starts plotting to that figure and I
> lose what's there. How do I force plotting to a figure even if user clicks
> on another? It's not in documentation for plot or figure command.

If you have a handle to the _axes_ on figure 2, use that.

% Create two axes and store their handles
ax1 = subplot(2, 2, 1);
ax2 = subplot(2, 2, 4);

% make the lower-right corner axes "active"
axes(ax2)

% plot into upper-left axes
plot(ax1, 1:10, 1:10)

% lower-right axes is still "active"
isLowerRightActive = isequal(gca, ax2)

% As we can see by plotting into it
plot(0:0.1:2*pi, sin(0:0.1:2*pi))

Specifying the figure is not sufficient, since there may be (and in my
example there are) multiple axes on the figure. PLOT, TITLE, XLABEL, and
YLABEL each allow you to specify the axes handle as the first input to plot
into or manipulate that specific axes.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Plot to a specific figure

From: Jeff

Date: 10 Jul, 2013 20:45:13

Message: 4 of 6

dpb <none@non.net> wrote in message <krk9bb$4hc$1@speranza.aioe.org>...
> On 7/10/2013 1:16 PM, Jeff wrote:
> > I activate and get the handle to a specific figure by
> >
> > h=figure(2);
> >
> > and plot to it using
> >
> > for tt = 0:1:T
> > plot(U(tt+1,:),'o'); axis(xs);
> > title(sprintf([txt ', t=%d (of %d)'], tt, T), ...
> > 'fontweight','b','FontSize',12);
> > xlabel(strXlbl,'fontweight','b');
> > ylabel(strYlbl,'fontweight','b');
> > drawnow;
> > end
> >
> > But if I click on figure 1, Matlab starts plotting to that figure and I
> > lose what's there. How do I force plotting to a figure even if user
> > clicks on another? It's not in documentation for plot or figure command.
>
> Au contraire... :)
>
> >> help figure
> figure Create figure window.
> figure, by itself, creates a new figure window, and returns
> its handle.
>
> figure(H) makes H the current figure, forces it to become visible,
> and raises it above all other figures on the screen. If Figure H
> does not exist, and H is an integer, a new figure is created with
> handle H.
> ...
>
> Once you've got the handle, use it as the second form to make it the
> current figure. To be effective if you have more than one that you want
> to use at a time then you need the handle variable to be a vector of
> handles and you must select the correct one for the desired target.
>
> You can retrieve the current figure w/ gcf and compare to the desired;
> if not the same execute the figure(h(index_desired)) before the plotting
> commences in the function. Or if the figure is known to be a specific
> one for the function, just hard code it in--if it exists it will make it
> current; if it doesn't it'll create a new one and make _it_ current...
>
>
> --

But that keeps bringing figure 2 back to the top. I guess I wanted a way to plot to figure 2 even while it stays in the background (my question was not clear).

Subject: Plot to a specific figure

From: dpb

Date: 10 Jul, 2013 21:14:31

Message: 5 of 6

On 7/10/2013 3:45 PM, Jeff wrote:
...

> But that keeps bringing figure 2 back to the top. I guess I wanted a way
> to plot to figure 2 even while it stays in the background (my question
> was not clear).

Ah...the transparent glistening orb was silent on that point! :) I
presumed it would be that you would want the one being updated to be
current and on top.

As Steven says, you then need more granularity -- keep the axes handles
(and you'll have to keep them straight as to which are on which figure)
and use them to plot directly to the desired one.

--

Subject: Plot to a specific figure

From: Jeff

Date: 11 Jul, 2013 01:06:07

Message: 6 of 6

dpb <none@non.net> wrote in message <krkirk$vav$1@speranza.aioe.org>...
> On 7/10/2013 3:45 PM, Jeff wrote:
> ...
>
> > But that keeps bringing figure 2 back to the top. I guess I wanted a way
> > to plot to figure 2 even while it stays in the background (my question
> > was not clear).
>
> Ah...the transparent glistening orb was silent on that point! :) I
> presumed it would be that you would want the one being updated to be
> current and on top.
>
> As Steven says, you then need more granularity -- keep the axes handles
> (and you'll have to keep them straight as to which are on which figure)
> and use them to plot directly to the desired one.
>
> --
Thanks. I hadn't seen Steven's reply when I replied.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us