Duplicating an imshow image into a new figure, without using imshow
Show older comments
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);
Accepted Answer
More Answers (1)
Walter Roberson
on 31 Dec 2015
You can apply hold() to a specific axes, and supply the axes as the Parent for imshow()
hold(subplot_axes, 'on');
imshow(TheImage, 'Parent', subplot_axes);
2 Comments
davidnhutch
on 31 Dec 2015
Walter Roberson
on 31 Dec 2015
Edited: Walter Roberson
on 11 Jun 2019
You cannot generally copy a figure into a subplot. figures can include menu items, uicontrol, uitable, uipanel, and various other items that are not permitted to be parented to an axes. Also in R2014a and earlier, legend are implemented as axes with linked properties.
You probably do not need to copy the fig into a new figure. You can openfig() it and specify 'new' (which is the default), store the resulting figure handle, and modify the contents of the result.
What might make sense is to create a uipanel in a new figure and copy the figure contents in to it, without the menus or toolbar, if you are trying to put the figure content "inside" a GUI.
pan2 = uipanel('parent', f2, 'Position', ....);
copyobj(findall(allchild(f1),'-depth', 0, '-not', 'type', 'uimenu', '-not', 'type', 'uitoolbar'), pan2)
This does not get all of the properties of the fig; for example it does not copy over the renderer mode or the current colormap (R2014a and earlier has only a single colormap per figure). But you need to ask yourself whether it is meaningful to copy those over if you are putting the content inside a container -- what if the other things you want to put into the new figure disagree?
But if you must...
%get names of settable properties
hu = get(0,'HideUndocumented');
set(0, 'HideUndocumented', 'off');
settable = set(f1);
set(0, 'HideUndocumented', hu);
%need to exclude some
fn = setdiff(fieldnames(pq),{'Children','CurrentAxes','CurrentCharacter','CurrentObject'});
for K = 1 : length(fn)
set(f2, fn{K}, get(f1, fn{K}));
end
We do not simply get(f1) to get all of the properties because not all of them can be set(); instead we set(f1) because if you set() without passing in at least parameter/value pair then what is returned is a struct with one field per parameter name (with the field content illustrating the possible values). But you need to weed out the Current* fields anyhow.
The bit about HideUndocumented has to do with copying properties whose existence is normally hidden from view... since we want all of the properties copied.
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!