subplot of already saved figures

467 次查看(过去 30 天)
Iyk
Iyk 2013-8-21
i have somany figures like dyke_tap, dyke_tbp,dyke_tcp and dyke_tdp...... they aare all bar plots and i want to have them side by side in other to compare them. i have seen ways of merging them, but that wont be good for what i want so i want to use a subplot. how can i use a subplot on already saved figures..... example ' having 'dyke_tap, dyke_tbp,dyke_tcp and dyke_tdp' side by side

回答(5 个)

laurie
laurie 2013-8-21
Here is a small bit of code that does just that for two saved figures. The figures need to be saved in the .fig format with this code (hgload only takes .fig figures as inputs), but there might be some way to adapt this code to make it work for other formats.
% Load saved figures
c=hgload('MyFirstFigure.fig');
k=hgload('MySecondFigure.fig');
% Prepare subplots
figure
h(1)=subplot(1,2,1);
h(2)=subplot(1,2,2);
% Paste figures on the subplots
copyobj(allchild(get(c,'CurrentAxes')),h(1));
copyobj(allchild(get(k,'CurrentAxes')),h(2));
% Add legends
l(1)=legend(h(1),'LegendForFirstFigure')
l(2)=legend(h(2),'LegendForSecondFigure')
  11 个评论
Yunyu Hu
Yunyu Hu 2020-5-5
This method works. But it does not copy the legend to the subplot

请先登录,再进行评论。


Farhad Sedaghati
Farhad Sedaghati 2015-6-22
编辑:Farhad Sedaghati 2015-6-22
You can use the following function to insert all the saved fig files next to each other: http://www.mathworks.com/matlabcentral/fileexchange/51236-subplot

Jesica Gomez
Jesica Gomez 2017-2-20
Hi, does it work for 3d graphs? I'm trying to use the code but it plots 2D graphs.
Thanks

Tasos Ampelas
Tasos Ampelas 2018-8-7
Hi everyone,
In the @laurie 's first answer code, is there a way to get only the final figure and not every single one that I have loaded to matlab?

Md Modassir Firdaus
Md Modassir Firdaus 2022-12-30
Hi everone,
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));

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by