How to show an existing figure inside my GUI
16 views (last 30 days)
Show older comments
I have button that runs an existing .m file. This .m file produces quite a few figures. I would like to show one of the figures inside the GUI and close the others. The GUI does not have a figure control, only an axis control. I am unable to find a solution. Some options I've seen are:
1.) h=gcf; h.Parent = handles.myAxis1.Parent (This produces a "Figure parent must be the root." error) It works if I use the example provided in which h=rectangle but it will not work for the many cases I tried in which the .m file produces plots (sometimes to pre-determined figure numbers)
2.) Edit the .m file and explicitly have the plot command plot to the GUI axes control. (I would like to avoid having to edit the .m files)
3.) Save the file as a bitmap, reload it inside the GUI, then delete the file. (This has the feel of a hack.)
Thank you.
0 Comments
Accepted Answer
Walter Roberson
on 1 Aug 2018
You have two challenges:
1) You have to find the right figure handle. If the figure has a name that shows up in the titlebar, then you can probably use
h = findobj(0, 'Name', 'The title goes here');
2) Figures cannot be put inside other figures. The closest you can get is to create a uipanel and move the contents of the figure inside the uipanel. This will not move any menus or handlers attached to the figures. For example, bode plots look like they are merely two axes of graphs, but really they have a bunch of behavior programmed in as well.
In the special case of a figure that has a single axes and no legend and no colorbar and no special behavior, you can sometimes get away with just setting the Parent property of the axes to be the figure you want it to appear inside, and then set the axes Position property to have it show up in the right place. This would cause the graphics to disappear from the original figure and show up in the new one; if you want the graphics to be in both places then you would copyobj() instead of setting the Parent property.
If the destination GUI already has an established axes that you want to move the graphics over to, then matters get a bit more complicated, as you need to set a whole bunch of properties, and that can be tricky sometimes. In the past I have posted code that tries to do that in an intelligent way, but lately I have started to wonder whether that code is correctly handling all of the details such as Colorspace and serializable children.
If there are colorbars or legends involved, then I am not sure at the moment what happens if you reparent the associated axes... or, for that matter, if the order of processing just happens to end up reparenting the colorbar or legend before the associated axes has moved.
1 Comment
More Answers (0)
See Also
Categories
Find more on Graphics Object Properties 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!