rotate/translate/zoom the two simultaneously generated figures within subplot
10 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
MarKf
2023-10-6
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 个评论
MarKf
2023-10-7
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!