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


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


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.

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:


This also works:


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

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.

give handle name to figure, give you a little example

  f1 = figure;
  f2 = figure;
  % edit image 1

Need Your Help

Angular - Websocket and $rootScope.apply()

javascript angularjs websocket

I'm currently playing around with an angular app that uses a websocket to communicate with the backend. I've had some trouble getting angular's data binding working correctly.

Getting All Users and All Roles through Identity visual-studio-2013

I have a new project i created in VS 2013. I am using the identity system and i'm confused how to get a list of all users to the application and all roles int he application. I am trying to creat...