MATLAB Answers

0

How to get a surface handle

Asked by Rafael Kübler on 21 Jul 2016
Latest activity Edited by Stephen Cobeldick on 21 Jul 2016
Hello together,
in the Matlab Help for surf it Shows the command
h = surf(...) which returns a handle to a chart surface graphics object.
I'm not quite sure, what h is. Whats the difference to the handle I get, calling gcf?
My question is: How can I get the same handle like h just after I plotted the surface.
So i want to plot the surface with
surf(...) (without using h = surf(....))
and then later get the handle created with h.
Is this possible?
Thank you for your help

  1 Comment

There are lots of graphics objects, each with handles. Why not read the documentation, which explains exactly how they are arranged:
Note that keeping and using the actual handle is always going to be faster an more robust. This is much better:
h = surf(...)
than anything involving gcf, gca, or findobj. You should think of these as conveniences for you while playing around in the command window, but never use them for serious code. They are far too unpredictable!

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 21 Jul 2016
 Accepted Answer

gcf returns the handle of the active figure. The figure is the entire window, including all menus, all buttons, all graphs, and so on. The handle returned by surf() is the handle just for the one surface.
If you do not record the handle at the time the surface is created, then you can use findobj to look for it:
findobj(gcf, 'type', 'surface')
provided the current figure has not changed.

  1 Comment

Guillaume
on 21 Jul 2016
At the end of the day what is the problem with using hsurf = surf(...)? It's easy and more importantly, it's safe. h is guaranteed to hold the handle to the surface you've created.
Sure, you could find it later using findobj(gcf, 'type', 'surface'), except that gcf may not be the figure where the surface was created, or the figure may have more than one surface, or ... Why worry, keep track of it from the start.
Note that gcf (and gca) are just as dangerous. gcf is the current figure. If you click on a different figure while your code is busy plotting, it will continue plotting on that different figure. It's always safer to keep track of the figure you've created (with hfig = figure).

Sign in to comment.