rotate/translate/zoom the two simultaneously generated figures within subplot

3 views (last 30 days)
Is there any way to rotate/translate/zoom the two simultaneously generated figures?
nodes = importdata("NODES.mat");
faces = importdata("FACES.mat");
figure
subplot(1,2,1)
plot3(nodes(:,1),nodes(:,2),nodes(:,3),'k.','Markersize',2);
hold on
trimesh(faces(:,:),nodes(:,1),nodes(:,2),nodes(:,3),'EdgeColor','k','Linewidth',0.1,'Facecolor','b','FaceAlpha',1)
hold off
axis equal
subplot(1,2,2)
plot3(nodes(:,1),nodes(:,2),nodes(:,3),'r.','Markersize',2);
hold on
trimesh(faces(:,:),nodes(:,1),nodes(:,2),nodes(:,3),'EdgeColor','k','Linewidth',0.1,'Facecolor','r','FaceAlpha',1)
hold off
axis equal

Answers (1)

MarKf
MarKf on 6 Oct 2023
You mean with the figure utilities after plotting beetween subplots? You can use linkaxes (or linkprop for a specific property) to have the subplots (or different figures) axes linked. Otherwise you can just plot in the same axes by translating your object.
nodess = load(websave('rd', "https://nl.mathworks.com/matlabcentral/answers/uploaded_files/1503874/NODES.mat"));
facess = load(websave('rd', "https://nl.mathworks.com/matlabcentral/answers/uploaded_files/1503869/FACES.mat"));
nodes = nodess.NODES; faces = facess.FACES;
figure
ax1 = subplot(1,2,1);
plot3(nodes(:,1),nodes(:,2),nodes(:,3),'k.','Markersize',2);
hold on
trimesh(faces(:,:),nodes(:,1),nodes(:,2),nodes(:,3),'EdgeColor','k','Linewidth',0.1,'Facecolor','b','FaceAlpha',1)
axis equal
ax2 = subplot(1,2,2);
plot3(nodes(:,1),nodes(:,2),nodes(:,3),'k.','Markersize',2);
hold on
trimesh(faces(:,:),nodes(:,1),nodes(:,2),nodes(:,3),'EdgeColor','k','Linewidth',0.1,'Facecolor','b','FaceAlpha',1)
xtransl = 20;
plot3(nodes(:,1)+xtransl,nodes(:,2),nodes(:,3),'r.','Markersize',2);
trimesh(faces(:,:),nodes(:,1)+xtransl,nodes(:,2),nodes(:,3),'EdgeColor','k','Linewidth',0.1,'Facecolor','r','FaceAlpha',1)
hold off
axis equal
linkaxes([ax1 ax2])
zoom(1.5)
  5 Comments
MarKf
MarKf on 7 Oct 2023
In the code above I translated the mesh simply by adding a value to the x dim node data. Matlab ofc has functions to translate, rotate, scale (resizing, what you likely actually meant with zoom above, misled me) polyshapes, surfaces, meshes, geometries, objects, structuring elements, robotics systems, etc... Regardless, one can easily make a function to apply a transform to several objects, which is what I meant just above, you can do the same operation to any object since you are scripting them.
You could transform the data and plot again (using a transformation matrix) but you could also create an object to which you parent your mesh and move around with hgtransform. This may be of interest to you, but unfortunately you cannot anyway make a single parent across subplots, that would be exactly what you asked (maybe someone knows the way but it's only a couple of lines more to apply to all). This also gives me the chance to try the attachment menu tool.
nodes = importdata("NODES.mat");
faces = importdata("FACES.mat");
figure
subplot(1,2,1)
g1 = hgtransform;
p1 = plot3(nodes(:,1),nodes(:,2),nodes(:,3),'k.','Markersize',2,'Parent',g1);
hold on
m1 = trimesh(faces(:,:),nodes(:,1),nodes(:,2),nodes(:,3),'EdgeColor','k','Linewidth',0.1,'Facecolor','b','FaceAlpha',1,'Parent',g1);
hold off
axis equal
subplot(1,2,2)
g2 = hgtransform;
p2 = plot3(nodes(:,1),nodes(:,2),nodes(:,3),'r.','Markersize',2,'Parent',g2);
hold on
m2 = trimesh(faces(:,:),nodes(:,1),nodes(:,2),nodes(:,3),'EdgeColor','k','Linewidth',0.1,'Facecolor','r','FaceAlpha',1,'Parent',g2);
hold off
axis equal
M = makehgtform('translate',[10 15 20],'scale',2,'xrotate',30);
set(g1,'Matrix',M)
set(g2,'Matrix',M)
% another way (no need to add the hgtransform parents) would be something like:
% newhandles = rotate({p1,m1,p2,m2},[1 0.5 0],30) does not work, but you can do it 1by1
% or make a funtion that does that as below (or extracts and applies a transform to all your data)
% newhandles = applyToall({p1,m1,p2,m2},M); % the plots3 p1 and p2 are not really visible
% function newhandles = applyToall(handles,M)
% for ic = numel(handles) %this funct could have more checks but quickly made
% ho = handles{ic};
% newhandles{ic} = applyTransform(ho,M); %you actually have to implement this
% end
% end
No need to linkaxes since they'd be moved around in the same way with the same transform. Also consider accepting the answer if that's what you were looking for or if it helped.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!