view() not returning [az, el] ??
Show older comments
According to this documentation, I believe that the view function should be returning a two element vector [azimuth, elevation]. However, I observed that it actually returns a 4 x 4 projection matrix. But it won't take one! So what do I need to do to get something out of view that it will accept later, to restore the viewpoint?
Here is a command window view of actions taken at a couple of breakpoints in my program:
(Breakpoint, when graph is showing a 3D plot)
K>> a=view(graph)
a =
0.7934 -0.6088 0 -0.0923
0.3044 0.3967 0.8660 -0.7835
0.5272 0.6871 -0.5000 8.3031
0 0 0 1.0000
(Another breakpoint, when graph is showing a 2D plot)
K>> a=view(graph)
a =
1.0000 0 0 -0.5000
0 1.0000 0 -0.5000
0 0 -1.0000 9.1603
0 0 0 1.0000
K>> view(graph, a)
Error using view>ViewCore
Argument must be scalar, or two-vector.
Error in view (line 93)
ViewCore(hAxes, viewArgs{:});
14 Comments
"According to this documentation, I believe that the view function should be returning a two element vector [azimuth, elevation]."
The VIEW documentation states that it returns the azimuth and elevation as two separate outputs:
Note that for non-scalar arrays of axes handles VIEW will throw an error when called with only one output argument:
ax1 = axes();
ax2 = axes();
A = view([ax1,ax2])
But when calling a multi-output function with only a single output argument it's standard (or at least typical) that the first ouput is returned as normal, unless documented otherwise, i.e.,
az = view(gca)
would be expected to return the usual az output.
Adam Danz
on 26 Apr 2024
Typically, yes. But in the case of view, the single-output option is not documented. If you look at the syntax block for view, it only lists the 2-output option. Compare that to sort which separately lists the 1-output and 2-output syntaxes to clarify Paul's point. But there are other examples such as bounds that only lists the 2-output syntaxes but does support the 1-output version Paul describes. So, I understand that confusion.

Just for completeness, here's the R2011a doc page for view which was the last time the single output option was documented. You can see separate syntax lines for the 1-output and 2-output syntaxes.

Hi Adam,
According to Ignore Function Outputs, "... you can request only the first N outputs of a function (where N is less than or equal to the number of possible outputs) and ignore any remaining outputs."
caz = view(gca)
to return caz, not T.
In other words, it's not necessary for the doc to list all possible output argument sequences as different options. It's nice that sort does list the single output option explicitly, but it's not required.
I don't see calling view with an array of axis inputs as a documented use, though it does seem to work with two outputs.
Ken
on 27 Apr 2024
Moved: John D'Errico
on 27 Apr 2024
John D'Errico
on 27 Apr 2024
You can never know why such a choice was made in the specifications for view, at least not without asking the person or people who made that exact decision many years ago. They may have had entirely valid reasons that you do not appreciate from your point of view (no pun intended.)
But if it terribly bothers you that view does not work as you would like, then you have several options.
- Write your own version of view. Call it myview, so it does not create unintended bugs in MATLAB. One that will behave exactly as you wish.
- Send in a feature request to the tech support line. I have always found them receptive to well thought out ideas for enhancement. If you think it a great idea, then get things started by writing your own version that would do as you think makes sense. Send that along as a suggestion. This will maximize the probability you get your idea accepted. Remember that the people who wrote and maintain the code for MATLAB are only human. They sometimes miss perfectly useful ideas. We all have blind spots.
Stephen23
on 27 Apr 2024
"... it's not necessary for the doc to list all possible output argument sequences as different options. It's nice that sort does list the single output option explicitly, but it's not required."
In general this is correct, but there are certainly some important exceptions where the number of output arguments changes what those arguments are, e.g. FIND, SVD, RAT, and as we learned today, VIEW.
When I wrote that I thought it self-evident that the doc is required to explicitly show the cases with different numbers of outputs when those cases change what those arguments mean, which is exactly what the doc shows for FIND, SVD, and RAT.
But the doc does not (anymore) show the single output case for VIEW, which means that calling it with one output should yleld the first output of whichever documented case to which the call resolves, which is the documented behavior under Ignore Function Outputs.
In the case of view, I don't know if the implemented behavior would ever cause a problem. For example, one might try code like this to increase the azimuth angle by 10 degrees
view(view+10,30)
which will result in an error because the inner call to view returns the T matrix, and a 4x4 matrix is not a valid input to view. But what if single-output view returned a scalar? OTOH, there would be problem for code that uses view in an expression that applies an operator that returns a scalar from a matrix.
I wonder why the single output case is no longer documented.
@Paul - I think your comment is exactly why they no longer document the single output case that returns an array - because they intend to remove that option in the future. And the reason they might remove that as an option is probably also in your comment, that returning one argument should be consistent with other tools in MATLB when possible, thus it should arguably return only the azimuth.
At the same time, there are other tools in matlab that return something different if called with only one output argument. Let me see if I can FIND one such tool. Oh yes. find does exactly this.
A = magic(5)
find(A == 3)
[I,J] = find(A == 3)
So in the single output case, find returns a linear unrolled index. But the two output case returns row and column indices.
Paul
on 27 Apr 2024
Yes, Stephen already pointed out the documented behavior of find and I acknowledged in my response comment that the doc page for FIND explicitly shows the difference between calling FIND with one output rather than two, so there's no question about the behavior for FIND.
The issue is what the user should expect as the return value for the not-explicitly-documented, single-output call to view. Based on the doc, the result should be the azimuth. Why do you think it's arguable?
MathWorks has had 12 years to remove that option. I wonder why they haven't already (I have my own speculation). And I wonder why they thought it wasn't a useful option for the user in the first place?
However, even though the expected behavior, based on the doc, of the single-output call to VIEW is not ambiguous IMO, the expected behavior of other functions when called with fewer outputs than documented is ambiguous. Consider this code with a call to ellip
fc = 300;
fs = 1000;
x = ellip(6,10,50,fc/(fs/2));
Should x be the return value of b as in the first documented use case, or should it be z as in the third?
I know what actually happens (x = b), but I don't know that there is a documented rule to formally answer that question.
For example, the rule could be:
a function called with fewer output arguments than any documented usage will be treated as if the function were called with the fewest number of documented output arguments and the unspecified outputs will be ignored in accordance with Ignore Function Outputs.
There may be other options as well. But AFAIK, such rule, whatever it may be, is not documented. A link to such a rule, should it exist, would be a valuable contribution to this thread.
John D'Errico
on 27 Apr 2024
MATLAB is not always perfectly consistent. Yes, one general rule is that when you return only one argument, it is often the first from the list of returned arguments. But a second rule that often seems to be used, is if you are asked to return only one argument, then the returned argument is something that tells you all the important information.
In the case of find, when only one argument is returned, we see the linear index, which tells you everything about the location of the found element(s). It seems they may have been thinking along those lines with view, at least when the code was written.
I have a feeling that mode will be phased out at some point though, based on the lack of documentation for it. That they have had years to do so is not uncommon. If you remove such an option, you want it to impact as few people as possible. And if nobody even knows that capability exsits, the number of people you will inconvenience is lessened.
Paul
on 27 Apr 2024
I ask again:
Based on the doc, the result [of single-output call to view] should be the azimuth. Why do you think it's arguable?
Walter Roberson
on 27 Apr 2024
Based on the doc, the result of a single-output call to view is undefined (but the most likely result is the azimuth)
Why is it undefined?
According to Ignore Function Outputs, "... you can request only the first N outputs of a function (where N is less than or equal to the number of possible outputs) and ignore any remaining outputs." followed by examples to show exactly what that clause means.
The doc page for the very example on that doc page, which is fileparts, only shows a use with three outputs. Yet calling FILEPARTS with one output is not defined?
Answers (1)
The azimuth and elevation outputs are returned as two separate outputs.
f = figure('visible','off');
ax = gca(f);
[az, el] = view(gca)
You discovered a little easter egg 😏
When only 1 output is requested, view returns a view matrix in a left handed coordinate system.
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!