Why does the "axis" function not give the expected output with a 3-D plot and "view(0,0)"?

2 views (last 30 days)
According to the documentation page 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.
However, "axis" does not always return what is expected. For example:
1. When plotting a line in 3-D and setting the view to (0,0), instead of returning a 4-element vector containing the limits of the x- and z-axis, it returns the x- and y-limits.
2. When using "view(eps,0)", the plot is indeed 3-D and the returned vector contains 6 elements as expected.
3. When using "view(eps,90)", the function does not append the z-limits despite this view being technically 3-D.
The "axis" function should return the limits of the actual view.
This odd behavior is illustrated by the following plot (generated by the attached script):

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 21 Feb 2019
There is a bug in the "axis" function of MATLAB R2014a.
In general, 2-D in MATLAB Graphics is defined as "view(0,90)" and 3-D is anything else (including XZ or YZ views -- see the function "is2d"). However, when using "view(0,0)", it should return a 6-element vector containing the limits of all three axis.
As a workaround you can use the "xlim", "ylim", and "zlim" functions to directly get the axis limits of each of the three axis. You would have to write your own tests to check which view the figure is in.
For example, the following function returns a 4-element vector for the XY, XZ, YZ, and YX views and a 6-element vector in other cases:
function out = getLimits(axH)
% Return a 4- or 6-element vector of limits for a single axis
% In the case of a 3-D view:
% - a is the x-limits
% - b is the y-limits
% - c is the z-limits
% In the case of a 2-D view:
% - a is the limits of the horizontal axis
% - b is the limits of the vertical axis
v = get(axH,'View');
% Return 3-D by default
a = get(axH,'XLim');
b = get(axH,'YLim');
c = get(axH,'ZLim');
if mod(v(1),180) == 0
a = get(axH,'XLim');
if mod(v(2),180) == 90
% XY view
b = get(axH,'YLim');
c = [];
elseif mod(v(2),180) == 0
% XZ view
b = get(axH,'ZLim');
c = [];
end
elseif mod(v(1),180) == 90
a = get(axH,'YLim');
if mod(v(2),180) == 0
% YZ view
b = get(axH,'ZLim');
c = [];
elseif mod(v(2),180) == 90
% YX view
b = get(axH,'XLim');
c = [];
end
end
out = [a b c];

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Tags

Products


Release

R2014a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!