Combining multiple existing plots

56 次查看(过去 30 天)
Morgan Dalziel
Morgan Dalziel 2022-3-13
编辑: Voss 2022-3-13
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.

回答(1 个)

Voss
Voss 2022-3-13
编辑:Voss 2022-3-13
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

类别

Help CenterFile Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by