Matlab - How to make a figure current? How to make an axes current?
Asked Answered
R

4

14

If f is the figure handle, I wanted to use plot3(..) on it just like I would use plot(..), but this didn't work:

>> plot3(f, t, real(Y), imag(Y))
Error using plot3
Vectors must be the same lengths.

Then I figured out that the way to do this is to:

  1. First make the relevant figure current.

  2. Then use the plot3(..) function.

I can find what the current figure is using gcf, but how do I make a figure current (via its handle)?

Runofthemill answered 8/10, 2012 at 4:43 Comment(2)
By the way, if we don't have a reference to an axes object, then we can (1) aa = findobj(gcf, 'type', 'axes'), then (2) cellfun(@(x) disp(x), get(aa, 'position')), from which we can tell which subplot is which, then (3) axes(aa(2)) for example, to make the second axes object current.Runofthemill
(While for figures, the current status seems to change with focus.)Runofthemill
S
34

This method has my personal preference:

set(0, 'currentfigure', f);  %# for figures
set(f, 'currentaxes', axs);  %# for axes with handle axs on figure f

because these commands are their own documentation. I find

figure(f)

and the like confusing on first read -- do you create a new figure? or merely make an existing one active? -> more reading of the context is required.

Stramonium answered 8/10, 2012 at 6:26 Comment(3)
Another advantage is that Matlab does not explicitly set the visibility to 'on' with this but retains the former visibility of the figure.Fagot
I take your point WRT readability, but is the 1st line really more readable? WTF is 0? Man I hate Matlab.Hugo
@Hugo Every programming language has its quirks, no need to start bashing. What's good is that 0 has been replaced in more recent MATLAB with groot -- the graphics root object. Makes more sense.Stramonium
R
9

It is actually as simple as feeding the f back into the figure(..) command:

figure(f)    %Makes the figure current.

Also, if I did something like this:

f = figure('IntegerHandle','off');    % With unique, non-reusable handle.
top = subplot(2, 1, 1);
bot = subplot(2, 1, 2);

Then I can make the axes top or bottom current by issuing a command like this:

subplot(top);

This also works:

axes(top);

But the two types of handles cannot be intermixed: axes(..) and subplot(..) work on axes handles, while figure(..) works on figure handles.

Runofthemill answered 8/10, 2012 at 4:43 Comment(1)
Damn, Matlab sucks sometimes. Seriously, Mathworks, why can't you add the axes_handle parameter to plot3 ?Bongbongo
A
4

While others have provided you exactly what you've asked for (how to make an axes or figure the current one). My preferred way for dealing with this, is to explicitly specify the parent of your plot in the call to plot3.

If you look at the documentation, you will see that you can specify the parent axes as the first parameter to the function. If looks like you attempted to do this in your example, but you provided a handle to a figure rather than an axes.

f = figure()
ax = axes('Parent', f)
im = plot3(ax, X, Y, Z);

Alternately, I prefer the explicit solution

im = plot3(X, Y, Z, 'Parent', ax)

The nice thing about this explicit parameter/value specification of the parent is that it is accepted by all graphics objects. Functions like plot and plot3 are actually helper functions that wrap the functionality of line and allow for the convention of passing the parent first. The parameter/value approach is widely accepted regardless of whether you're working with a higher level function (plot, plot3, imshow) or the lower level objects (line, image, etc.)

The two benefits here are that you remove the overhead of MATLAB trying to figure out where to put your plot and also, it prevents MATLAB from having to change which figure is currently displayed, forcing a re-rendering which is one of MATLAB's slowest tasks.

Anon answered 10/1, 2016 at 13:46 Comment(0)
G
0

give handle name to figure, give you a little example

  f1 = figure;
  imshow(image1);
  f2 = figure;
  imshow(image2);
  % edit image 1
  figure(f1);
  text(2,3,'done');
Glazunov answered 8/5, 2014 at 17:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.