Evgeni Sergeev
Evgeni Sergeev

Reputation: 23611

Matlab - How to make a figure current? How to make an axes current?

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)?

Upvotes: 15

Views: 80690

Answers (4)

Suever
Suever

Reputation: 65460

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.

Upvotes: 4

neouyghur
neouyghur

Reputation: 1647

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');

Upvotes: 0

Evgeni Sergeev
Evgeni Sergeev

Reputation: 23611

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.

Upvotes: 9

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38042

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.

Upvotes: 34

Related Questions