Combining multiple existing plots
56 views (last 30 days)
Show older comments
Hi,
I have generated multiple MATLAB figures using a code that I have to manually edit each time. Just wondering if there is a way I can combine these pre-existing figures into one figure that shows the data overlapped from each figure.
0 Comments
Answers (1)
Voss
on 13 Mar 2022
Edited: Voss
on 13 Mar 2022
You can try doing something like what follows. This assumes each figure has one axes and you want to put all graphics objects from every figure's axes into a single axes in a new figure. Axes properties such as labels and titles - as well as any legends - will not be preserved, but see if this is at least a step in the right direction. (If the figures you have are saved as .fig files you can use the same approach - just open() each one and subsequently delete() it inside the for loop.)
% first, I create a couple of figures containing some lines
fig_1 = figure();
plot(1:10);
fig_2 = figure();
plot(2:11);
drawnow();
% now combine the figures' contents into a new figure
% specify which "old" figures to use
figs = [fig_1 fig_2];
% make a "new" figure
f = figure();
% get the new figure's axes
ax = gca();
% for each old figure
for ii = 1:numel(figs)
% find all the graphics objects in the figure's axes
obj = findall(get(figs(ii),'CurrentAxes'));
% remove the axes itself
obj(strcmp(get(obj,'Type'),'axes')) = [];
% copy the objects to the new figure's axes
copyobj(obj,ax);
end
0 Comments
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

