How to transfer axes from GUI to figure and save as PDF

12 views (last 30 days)
I have a GUI (handle = main) that allows the user to select some data and then plots it on the axis built into the GUI (handle = axes1)
After the plot is generated, I'm trying to save the image as a PDF, but I'm running into the following issues:
1) copyobj(handles.axes1,h_fig) doesn't keep both primary and secondary axes, and upon closing h_fig, the secondary axis from the GUI disappears!
2) copyobj(get(main,'children'),h_fig) gets EVERYTHING form the GUI including the push buttons, listboxes, etc which I don't want. Even this method, the positioning of the legend isn't copied over correctly.
3) i tried to use the export_fig script submission from Oliver Wood and it has the same issue. I tried both export_fig, as well as isolate_axes functions, both only keep the primary axis info.
Am i doing something wrong? is the secondary axes hidden when it gets copied? and once copied, does the visibility get set to invisible on the original GUI axes?
Any help is greatly appreciated!

Answers (2)

Mahdi
Mahdi on 25 Mar 2013
I had this problem a while ago. I couldn't figure out how to solve it.
I figured the best way was to open up a new figure (In the callback function, open a new figure, say figure 1 - type figure(1)), then plot the same data that you want, save figure 1, then close figure(1). This is definitely inefficient though, but I haven't found a different way.

David C
David C on 25 Mar 2013
Mahdi,
I figured out the solution.
First you have to find all the axes on the GUI that are NOT legends with the command:
H1 = findobj(maingui,'type','axes','-not','tag',legend');
then get the legend
L1 = findobj(maingui,'tag','legend');
Then use copyobj to copy those handles from the GUI to the new figure, and finally save the figure.
After debugging, I realized that if you use plotyy to make plots on a axes, the primary and secondary axis have different handles (something I should have realized since that is one of the ways to call the plotyy function)
Anyway, after finding all the objects and copying them over, it was a matter of setting their 'location' parameter and fitting them on a 8.5x11 paper which took some finessing.
I think this is still pretty inefficient cause my code is almost half a page long, but it gets the job done for now.
  1 Comment
Oliver Woodford
Oliver Woodford on 19 Jun 2013
David: If you call export_fig([h1 h2], 'test.jpg'), where h1 and h2 are the two axes handles returned by plotyy, does that work?

Sign in to comment.

Categories

Find more on Two y-axis 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!