Camera and view function and properties
12 views (last 30 days)
Show older comments
I've been trying to understand the view and camera functions and created the following code for illustration:
peaks(30);
ax=get(gca);
% Display values
disp('Initial values')
dispprops(ax)
% Change target
ax.CameraTarget = [0 0 10];
disp('Target changed by property')
dispprops(ax)
camtarget([0 0 20])
disp('Target changed by function')
dispprops(ax)
% Change position
ax.CameraPosition = [-30 -40 70];
disp('Position changed by property')
dispprops(ax)
campos([-30 -40 80])
disp('Position changed by function')
dispprops(ax)
% Change view
ax.View = [45 90];
disp('View changed by property')
dispprops(ax)
view([60 60])
disp('View changed by function')
dispprops(ax)
function dispprops(ax)
viewfromax = ax.View %#ok
[azfromfun,elfromfun] = view;
disp(['viewfromfun = ' num2str(azfromfun) ',' num2str(elfromfun)])
posfromax = ax.CameraPosition %#ok
disp(['posfromfun = ' num2str(campos)])
targetfromax = ax.CameraTarget %#ok
disp(['targetfromfun = ' num2str(camtarget)])
end
There are two things that seem odd to me:
1) Changes via the ax properties are stored, but don't do anything in the plot (even if the respective modes are set to manual). On the other hand, changes via functions are not stored in the properties, but do modify the plot. So what are the properties actually good for?
2) Changes to position and target do not change the view values, but view on the other hand does change target and position. Shouldn't the dependency be mutual? I. e. aren't azimuth and elevation determined by camera position and target (since target and position are independent from each other, c. f. https://mathworks.com/help/matlab/creating_plots/defining-scenes-with-camera-graphics.html)?
0 Comments
Accepted Answer
Voss
on 1 Apr 2022
Edited: Voss
on 1 Apr 2022
When you do
ax = get(gca);
you are storing the axes properties in a struct called ax. (In this case these are the initial properties of the axes, since you do this before anything else.) Setting values of fields in ax doesn't affect the axes itself:
ax.View
ax.View = [0 0]
% actual View is still [0 90] because 'View' was set
% in ax, which is a copy of the axes properties
get(gca,'View')
Store the axes itself instead:
ax = gca;
and the properties will be accurate after they are set, regardless of how they are set.
peaks(30);
% ax=get(gca);
ax = gca;
% Display values
disp('Initial values')
dispprops(ax)
% Change target
ax.CameraTarget = [0 0 10];
disp('Target changed by property')
dispprops(ax)
camtarget([0 0 20])
disp('Target changed by function')
dispprops(ax)
% Change position
ax.CameraPosition = [-30 -40 70];
disp('Position changed by property')
dispprops(ax)
campos([-30 -40 80])
disp('Position changed by function')
dispprops(ax)
% Change view
ax.View = [45 90];
disp('View changed by property')
dispprops(ax)
view([60 60])
disp('View changed by function')
dispprops(ax)
function dispprops(ax)
viewfromax = ax.View %#ok
[azfromfun,elfromfun] = view;
disp(['viewfromfun = ' num2str(azfromfun) ',' num2str(elfromfun)])
posfromax = ax.CameraPosition %#ok
disp(['posfromfun = ' num2str(campos)])
targetfromax = ax.CameraTarget %#ok
disp(['targetfromfun = ' num2str(camtarget)])
end
3 Comments
Voss
on 2 Apr 2022
It looks like it's because the properties don't update instantly, but only when the next drawnow (or other command that processes the graphics event queue such as figure or pause) occurs.
Here's an example where CameraPosition is set and then View is queried afterward. In Case 1 no drawnow is used and View remains at its old value, and in Case 2 drawnow is used in between setting CameraPosition and getting View, and View shows the updated value:
% First, a reference figure with default axes 'View' and
% 'CameraPosition' properties shown:
figure();
peaks(30);
ax = gca();
get(ax,'View')
get(ax,'CameraPosition')
% Case 1:
% when setting 'CameraPosition', 'View' property doesn't change
% (but actual apparent 'View' does):
figure();
peaks(30);
ax = gca();
get(ax,'View')
get(ax,'CameraPosition')
set(ax,'CameraPosition',[0 -40 80])
% still the old View (but clearly the surface is seen
% from a different angle than in the first figure)
get(ax,'View')
% Case 2:
% putting a drawnow() after setting 'CameraPosition'
% causes 'View' property to show the updated value:
figure();
peaks(30);
ax = gca();
get(ax,'View')
get(ax,'CameraPosition')
set(ax,'CameraPosition',[0 -40 80])
drawnow()
get(ax,'View') % now View is accurate
More Answers (0)
See Also
Categories
Find more on Graphics Performance 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!