What is the best way to transfer ALL the figure data to GUIDE (GUI) axes?

Hello,
I have the attached figure. It has:
  • xlabel & ylabel
  • xAxis & yAxis
  • title
  • grid
  • colorbar
  • surf (x,y,z) plot
I have handle to this figure in my code.
How can I insert ALL_OF_IT into my GUIDE (GUI) axes object?
I've tried to use copyobj, BUT:
  1. it copies only the surf map itself (with some wired change of colormap... but I suppose I can live with that).
  2. I don't know how to copy also all the other stuff (xlabel , title , color bar , etc...)
I've tried to save my figure as an image file (tiffn), and then load it (imread) onto the axes. It works basically, but it makes it in a very poor/small resolution.
Also, I want to be able to shift it back from my GUI axes to an external figure. So having it stored as an image on my axes is far from optimal (I'd like to be able to zoom, go over data points, etc...)
Any Ideas?
THANKS !!!

 Accepted Answer

fig1 = figure();
ax1 = axes(fig1);
[X, Y] = meshgrid(linspace(-5,5,50));
Z = tan(X).*cos(Y);
surf(ax1, X, Y, Z, 'edgecolor', 'none');
xlabel(ax1, 'x');
ylabel(ax1, 'y');
colorbar(ax1);
grid(ax1, 'on');
title(ax1, 'This is the original');
fig2 = figure();
copyobj(fig1.Children, fig2)
ax2 = gca(fig2);
title(ax2, 'This is the copy')

6 Comments

I have only fig1_handle (my original fig), and gui axes handle (in guide... for example: handles.twoD_axes
please have a look at the attached GUI layout:
I want to copy my fig1 (full content) to my 2D plot axes (defined by handles.twoD_axes).
I can't do this with your example. I've tried the following code:
heatMapFigHandle = plotWM(heatMap_inputMat , '3 Sigma per site'); % this is my fig_1 handle
my_ax = gca;
my_ax_child = get(my_ax , 'Children');
copyobj(my_ax_child , handles.twoD_axes)
But, it copied only the surf data, without all the peripherals. See image below:
As you can see from previosuly attached figure img, colormap is different (but ok...).
I'm more worried regarding:
How do I copy also the: colorbar , x & y label , x & y axis.
Am I missing something?
Notice that the copyobj was on the children of the figure, not of the axes. colorbar in particular is not part of the axes. title and labels are not children of the axes, they are properties of the axes. This causes complications.
%creating source to copy for demo purposes
heatMapFigHandle = figure('Name', 'original');
ax1 = axes(heatMapFigHandle);
[X, Y] = meshgrid(linspace(-5,5,50));
Z = tan(X).*cos(Y);
surf(ax1, X, Y, Z, 'edgecolor', 'none');
xlabel(ax1, 'x');
ylabel(ax1, 'y');
colorbar(ax1);
grid(ax1, 'on');
title(ax1, 'This is the original');
%creating destination to copy to for demo purposes
twoD = figure('Name', 'Destination GUI');
handles.twoD_axes = axes(twoD);
%if this was not a demo you would have instead called
%{
heatMapFigHandle = plotWM(heatMap_inputMat , '3 Sigma per site'); % this is my fig_1 handle
%}
twoD_figure = ancestor(handles.twoD_axes, 'figure');
HM_ax = gca(heatMapFigHandle);
%what can be written?
copy_details_only = {'Title', 'XLabel', 'YLabel', 'ZLabel'};
copyable_properties = setdiff(fieldnames(set(HM_ax)), ...
[{'Children', 'Parent', 'XAxis', 'YAxis', 'ZAxis'}, copy_details_only]);
%clone properties
set(handles.twoD_axes, copyable_properties, get(HM_ax, copyable_properties));
%copy the things that are inside the axes
copyobj(HM_ax.Children, handles.twoD_axes);
%colorbars have to be copied with axes, but we are not copying axes
cb = findobj(heatMapFigHandle, 'type', 'colorbar');
if ~isempty(cb)
copyable_properties = setdiff(fieldnames(set(cb)), {'Children', 'ContextMenu', 'Parent' });
twoD_cb = colorbar(handles.twoD_axes);
set(twoD_cb, copyable_properties, get(cb, copyable_properties));
end
%text objects have to be copied, otherwise they disappear from original
for K = 1 : length(copy_details_only)
propname = copy_details_only{K};
srcobj = HM_ax.(propname);
destobj = handles.twoD_axes.(propname);
copyable_properties = setdiff(fieldnames(set(srcobj)), {'Children', 'Parent'});
set(destobj, copyable_properties, get(srcobj, copyable_properties));
end
The second one is not really the original... the title got copied too, as you requested.
Thank you for the detailed response, but unfortunately it didn't work. Few questions/remarks:
  1. ax1 & HM_ax are the same, right? I got error for ax1 ("output has too many arguments"... MATLAB 2015b). Anyway, you don't use ax1 anywhere down the code...
set(twoD_cb, copyable_properties, get(cb, copyable_properties));
gave me the following error:
Error using matlab.graphics.illustration.ColorBar/set
The UIContextMenu's parent figure is not the same as the parent figure of this object
Eventually, all those lines of code somehow made my 2D axes on the gui layout to disappear, and be translated to some small bottom-left corner... in some weird way... please see image below (marked with Blue pen):
I don't know if it's related, BUT, can you please create an example where the following conditions exist:
  1. source is a stand alone figure (with 'Visible' set to 'off'... not crticial of course, I can make it visible again if required) - you can see an example in my attached file: my_fig.jpg
  2. destinated is an axes object, which is part of a MAIN GUIDE figure, for the sake of the example it's handle called: handles.twoD_axes (I mean it's axes type object that I drag & drop onto my GUIDE GUI main figure pallate.
All I want to do, is a FULL copy-paste of ALL contents of my source FIGURE into my destinates axes.
CAN this be done? Do I need to configure something before that? Maybe units type or something like that? I don't know...
This portion of the code:
%creating source to copy for demo purposes
heatMapFigHandle = figure('Name', 'original');
ax1 = axes(heatMapFigHandle);
[X, Y] = meshgrid(linspace(-5,5,50));
Z = tan(X).*cos(Y);
surf(ax1, X, Y, Z, 'edgecolor', 'none');
xlabel(ax1, 'x');
ylabel(ax1, 'y');
colorbar(ax1);
grid(ax1, 'on');
title(ax1, 'This is the original');
%creating destination to copy to for demo purposes
twoD = figure('Name', 'Destination GUI');
handles.twoD_axes = axes(twoD);
exists only to have some data to demonstrate the process on. The only part of it that carries over to the remainder is that that heatMapFigHandle and handles.twoD_axes are used. Everything else about the first part is a "black box" as far as the second part is concerned. The entire first part would be replaced with
heatMapFigHandle = plotWM(heatMap_inputMat , '3 Sigma per site'); % this is my fig_1 handle
in your actual code (that and the fact that handles.twoD_axes is assumed to already exist.)
thanks, I've got exactly the result I wanted with the following code:
heatMapFigHandle = plotWM(heatMap_inputMat , '3 Sigma per site'); % my figure handle
heatMap_ax = gca(heatMapFigHandle);
cla(handles.twoD_axes , 'reset')
colormap(handles.twoD_axes , 'jet')
copyobj(heatMap_ax.Children, handles.twoD_axes);
colorbar(handles.twoD_axes)
When you do that, the properties of the axes such as the labels and limits and axis direction and view do not copy over; also any special colormap customization will not get copied.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!