MATLAB: Combining Two Plots into One Won’t Work!

combinemerge

Hello dear experts,
I am trying to combine two figures into one, and I am using the solution already provided here, however, it is not working for me! I really appreciate it if you help me with that. Here I attached two figures as an exmaple.
This is the code that I am using:
fh1 = open('f1.fig');
fh2 = open('f2.fig');
ax1 = get(fh1, 'Children');
ax2 = get(fh2, 'Children');
ax2p = get(ax2(1),'Children');
copyobj(ax2p, ax1(1));

Best Answer

Not trivial, however also not difficult.
F{1} = openfig('f1.fig');
F{2} = openfig('f2.fig');
figure
hold on
for k1 = 1:numel(F)
Ax = F{k1}.CurrentAxes;
lines = findobj(Ax, 'Type','Line');
sktrs = findobj(Ax, 'Type','scatter');
for k2 = 1:numel(lines)
xl{k2} = lines(k2).XData; % Force Row Vector



yl{k2} = lines(k2).YData; % Force Row Vector
xs{k2} = sktrs(k2).XData; % Force Row Vector
ys{k2} = sktrs(k2).YData; % Force Row Vector
end
xvl = cell2mat(xl); % Extract From Cell Array



yvl = cell2mat(yl); % Extract From Cell Array
xvs = cell2mat(xs); % Extract From Cell Array
yvs = cell2mat(ys); % Extract From Cell Array
p(k1) = plot(xvl, yvl, 'DisplayName',sprintf('Fig(%d) Line',k1), 'LineWidth',1);
s(k1) = scatter(xvs, yvs, 'p', 'DisplayName',sprintf('Fig(%d) Scatter',k1));
end
hold off
grid
title(Ax.Title.String)
xlabel(Ax.XLabel.String)
ylabel(Ax.YLabel.String)
legend('Location','SE')
.