Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store MATLAB 3d viewport

Tags:

plot

matlab

I am using MATLAB to display 3d data. I use the GUI to change to view angle, zoom and pan. How can I store this view and later apply it to another figure (containing more or less the same data)? view gives me a matrix, but how can I apply this to another figure?

Thanks a lot!

like image 630
Philipp Avatar asked Feb 23 '11 11:02

Philipp


3 Answers

To use the current view angle on another figure, you could do:

% call when the source axes is the current axes
[az, el] = view; 
% call when the target axes is the current axes
view (az, el);

Or, you could do the same using get and set of the view property.

However, to apply all the view properties you mentioned together, it is easier to use Matlab's built-in 'generate m file' option - when you have the 3D figure the way you want it, go to file->Generate m-file, and an m-file will be created that gets the 3D data as an input, and apply the settings.

Another option is to save these relevant settings yourself (just inspect the m-file generated):

plot3(sin(t),cos(t),t);
pba = get(gca, 'PlotBoxAspectRatio');
dar = get(gca, 'DataAspectRatio');
cva = get(gca, 'CameraViewAngle');
cuv = get(gca, 'CameraUpVector');
ct = get(gca, 'CameraTarget');
cp = get(gca, 'CameraPosition');

and then to apply it to the current axes (assuming the target axes is the current one):

set(gca, 'PlotBoxAspectRatio',pba);
set(gca, 'DataAspectRatio',dar);
set(gca, 'CameraViewAngle',cva);
set(gca, 'CameraUpVector',cuv);
set(gca, 'CameraTarget',ct);
set(gca, 'CameraPosition',cp);
like image 74
Itamar Katz Avatar answered Sep 18 '22 09:09

Itamar Katz


To store and apply view see Itamar's answer.

For zoom and pan you just need to store axes limits. Use xlim, ylim and zlim or corresponding axes properties (XLim, etc.) with get/set.

For current axes to store limits:

xl = xlim;
yl = ylim;
zl = zlim;

Or with axes properties:

xl = get(gca,'XLim');

To apply to axes on another figure:

xlim(new_axes_handle,xl) % you can skip new_axes_handle for current axes
ylim(new_axes_handle,yl)
zlim(new_axes_handle,zl)

Or

set(new_axes_handle,'XLim',xl,'YLim',yl','ZLim',zl)

By the way, in addition to Itamar's answer, you can get all axes properties at once into a structure:

ax_properties = get(gca);

Remove the fields you don't want to apply to the new axes, then use SET:

set(new_axes_handle,ax_properties)

Be careful since the structure will contain UserData as well. Remove this field first. You can do it by the code:

rmfield(ax_properties,'UserData')
like image 2
yuk Avatar answered Sep 19 '22 09:09

yuk


OMG, manual adjustment and the generate M-File option is my hero. If only I knew from the beginning that all I had to do was this:

axes1 = axes('Parent',Plot_ele,'PlotBoxAspectRatio',[1 1.70454545454545 1.7],...
    'DataAspectRatio',[1 1 1],...
    'CameraViewAngle',7.48227189414101,...
    'CameraUpVector',[-0.0256575066196788 0.989185543639328 -0.144407938178721],...
    'CameraTarget',[255.013054349713 397.874703616223 449.003273637903],...
    'CameraPosition',[1445.8877301745 1407.25270740567 7151.59363497921]);
    % xlim(axes1,[0 528]); % uncomment to preserve axes x-limits
    % zlim(axes1,[0 897.6]); % uncomment to preserve axes z-limits
hold(axes1,'all');
like image 1
Mike P. Abell Avatar answered Sep 22 '22 09:09

Mike P. Abell