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