Getting figure and adding it to guide axes

4 views (last 30 days)
I have been given some code which generates a plot inside a figure when runned. I'm designing a GUI that should include this figure in the axis. The problem is that I'm able to get the handle of the figure the code generates but I'm not able to put it inside the axes from my guide. Right now I have this part of the code but I'm not able to make the final step to put h/figure(1) into the axes1.
code.run() %Generates figure(1)
h = code.getFigureHandle(); %I get the handle
axes(handles.axes1);

Accepted Answer

Walter Roberson
Walter Roberson on 27 May 2015
No, that is not possible. A figure cannot be contained in any other object. Figures also always have children objects, some of which cannot be contained in an axes; for example in R2014a, if you
h = figure(1);
then even without drawing in it, it will have children of type uimenu, uipushtool, uitogglesplittool, uitoggletool, uitoolbar, none of which can be children of axes.
It is common for figures to have multiple axes; for example legend() creates additional axes.
For these reasons, you should not just arbitrarily move a figure or the contents of a figure into an axes.
What is usually reasonable to do is to create a uipanel, and to run through all of the non-ui* direct children of the figure and set() the Parent of the object to be the uipanel.
code.run() %Generates figure(1)
h = code.getFigureHandle(); %get the handle
panelh = handles.uipanel1; %presuming you already created it
hc = findall(h, '-depth', 1);
hct = get(hc,'type');
fighand = strcmp(hct, 'figure');
uihand = strncmp(hct, 'ui', 2);
hc(fighand | uihand) = []; %get rid of those
for ch = hc
try; set(ch,'Parent', panelh); catch; end
end
  1 Comment
ricard molins
ricard molins on 27 May 2015
I got the idea of why my way was not working. Thanks for the code as I wouldn't have been able to do it without your help.
Worked like a charm

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps 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!