Clear Filters
Clear Filters

Put existing 3D figures on subplot

39 views (last 30 days)
John Willis
John Willis on 9 Mar 2022
I have a couple figures already existing in my program but want to add them together to a subplot. e.g., put fig 1 and fig 5 on a subplot. I found some code and modified it slightly:
%% Original 3D Plot
[X,Y,Z] = peaks;
plot = surf(X,Y,Z);
%% Copy plot to subplots
f = figure;
s1 = subplot(1,3,1);
s2 = subplot(1,3,2);
s3 = subplot(1,3,3);
copyobj(plot,s1);
copyobj(plot,s2);
copyobj(plot,s3);
%% Set different viewing angle for each subplot
view(s1,0,90); title(s1,'view(0,90)');
view(s2,90,0); title(s2,'view(90,0)');
view(s3,0,0); title(s3,'view(0,0)');
My problem is it loses the third dim and plots everything 2D.

Answers (1)

Md Modassir Firdaus
Md Modassir Firdaus on 30 Dec 2022
Hi everyone
You can try this code to create single figure having subplots. Here single plot is in subplot becouse it does not saved figure 1. First save the created plot then comment "savefig".
clc;
close all;
clear;
%%
z1=peaks;
z2=z1+randn(size(z1))/5;
figure(1)
surf(z1) %creating first figure
savefig('PeaksFile1.fig') % save the figure once then comment it
%%%%
figure(2)
surf(z2,'FaceColor','r') %creating second figure
savefig('PeaksFile2.fig')% save the figure once then comment it
%% Loading saved above figure
f1=hgload('PeaksFile1.fig');
f2=hgload('PeaksFile2.fig');
%% creating subplot
figure(3)
h(1)=subplot(1,2,1);
view(3)
grid on
h(2)=subplot(1,2,2);
view(3)
grid on
%%
copyobj(allchild(get(f1,'CurrentAxes')),h(1));
copyobj(allchild(get(f2,'CurrentAxes')),h(2));

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!