4

As stated, what is the easiest way to determine programmatically if a Matlab axes object is a 2D or 3D plot?

crobar
  • 2,810
  • 4
  • 28
  • 46
  • 3
    All axes are 3d, but Nzbuu's answer will indicate that you're viewing it as 2d. – Edric Nov 09 '11 at 14:41
  • Yes, I knew this, but there are differences in what happens when you use zoom on an axis viewed in 3D rather than 2D which is why I need to know. – crobar Nov 09 '11 at 16:22

2 Answers2

4

From the documentation for the axis function:

V = axis returns a row vector containing the scaling for the current plot. If the current view is 2-D, V has four components; if it is 3-D, V has six components.

Thus, you can get the dimension of the axes by calling

plot_dim = numel(axis)/2;

It will return 2 for 2D and 3 for 3D.

If you have a reference ax to an axes object, you can modify the above code by passing ax to axis:

plot_dim = numel(ax)/2;
Paul Wintz
  • 2,542
  • 1
  • 19
  • 33
2

Examine the output of [az,el] = view. If it's 2D, then el == 90.

Nzbuu
  • 5,241
  • 1
  • 29
  • 51
  • although technically the answer to my question is `[az,el] = view(hax)` where `hax` is a handle to the axes of interest. Thanks! – crobar Nov 09 '11 at 16:19
  • as you can see from this [example](http://stackoverflow.com/questions/7960059/matlab-plotting-saving-x-y-views-of-mesh-function-in-subplots/8059484#8059484), there are more 2D cases (as well as their rotated versions) – Amro Nov 09 '11 at 19:04
  • The answer by Jeff Fessler (https://stackoverflow.com/a/40723959/6651650) is a more robust solution. – Paul Wintz Sep 03 '21 at 21:14
  • 1
    You can rotate a 3D plot to have `el=90`, it’s still a 3D plot. – Cris Luengo Sep 05 '21 at 15:25