MATLAB: Duplicating an imshow image into a new figure, without using imshow

duplicate figureimshowmerge figures

I use imshow to show the 'coins.png' built-in image. I'm trying to duplicate that image perfectly into another figure. The below code ALMOST works, but there are still differences:
  • The colormap is not copied over, despite my effort on the last line to do so
  • The new image isn't the same size, despite copying over the 'Position' property
  • The new image is arbitrarily stretchable, unlike the old image (try it and see)
  • The new image has the thick black axis UNDER the image, while the old image has it on top of the image
  • The image is flipped
How can I fix the above?
In case you are wondering, no I can't just call imshow('coins.png') on the second figure. Because, in my case I have a more complicated situation (I have an existing .fig file with an image inside it that I am recalling into a subplot, and I need to replicate the axis in the subplot axis. If I just call imshow on the second figure's subplot I lose all the associated formatting such as axis labels and axis titles. I have many such images to do, so can't do it manually. Anyway if I can solve the above problem I could do this.)
Here's the code:
clear;
close all;
% Old figure
f1 = figure;
imshow('coins.png')
axis on;
colormap(jet);
old_ax = gca;
set(old_ax,'LineWidth',2);
% New figure -- should be a perfect copy of the old
f2 = figure;
new_ax = gca;
copyobj(get(old_ax,'children'),new_ax);
% Copy over a bunch of properties
properties = {'Title','Box','BoxStyle','LineWidth','XLabel','YLabel',...
'XLim','YLim','CLim','CLimMode','XTick','YTick','TickDir',...
'FontName','FontSize','Position'};
for i = 1:numel(properties)
prop_str = char(properties(i));
set(new_ax,prop_str,get(old_ax,prop_str));
end
% Copy over the colormap too
cm = colormap(old_ax);
colormap(new_ax,cm);

Best Answer

This seems to work.
% Create the Old figure
f1 = figure;
imshow('coins.png')
axis on;
colormap(jet);
oldColorMap = colormap();
xlabel('x');
ylabel('y');
title('Coins Figure');
old_ax = gca;
set(old_ax,'LineWidth',2);
hold on;
% New figure -- should be a perfect copy of the old
f2 = figure;
new_ax = gca;
copyobj(get(old_ax, 'children'), new_ax);
% Copy over the figure size
fig1Position = get(f1, 'Position');
set(f2, 'Position', fig1Position');
% Copy most of the properties
oldProperties = get(old_ax);
figureFieldNames = fieldnames(oldProperties)
for k = 1:length(figureFieldNames)
thisProperty = figureFieldNames{k};
if ~isempty(strfind(thisProperty, 'Fcn'))
continue;
end
if strcmpi(thisProperty, 'Tag') || strcmpi(thisProperty, 'Parent') || strcmpi(thisProperty, 'Children')
continue; % Skip the tag, parent, and children properties
end
fprintf('About to try to set %s\n', thisProperty);
try
new_ax.(thisProperty) = old_ax.(thisProperty);
catch
% Some properties are readonly so this will skip them
end
end
% Copy over the colormap too
colormap(new_ax, oldColorMap);
The only thing I can't figure out is why it removes the xLabel, yLabel, and title from the old figure when it applies them to the new figure. It should just add them to the new figure, not remove them from the old figure. Maybe Walter knows.
Related Question