How can I save an axes which has multiple coordinate systems?

50 views (last 30 days)
I have an UI which contains an axes with two Y-axis. I used the yyaxis command. How can I save the axes to png or an other data format? The problem is that when I save the whole figure, it will contain also parts of the UI Objects. The panels for example. Instead I want to save just the axes. The advice I found, was to copy the axes to a new figure and save it there. Sadly, copyobj is not working with axes with multiple coordinate systems. Can you help me?
Edit: its yyaxis not yyaxes
  3 Comments
dpb
dpb on 19 Aug 2018
Edited: dpb on 19 Aug 2018
Was going to say "must not be doing it right" until was going to show--
>> hAx(1)=gca;
>> yyaxis right
>> hAx(2)=gca;
>> figure
>> copyobj(hAx,gcf)
Error using copyobj
Cannot copy the same object more than once to the same parent. Try calling COPYOBJ twice instead.
>> copyobj(hAx(1),gcf)
Error using copyobj
Object Copy of Axes with multiple coordinate systems is not supported.
>>
What a kick in the teeth; this is nasty. Add some ease of use over plotyy but lose some flexibility. "There is no free lunch."
All I can suggest is if this is important-enough without the accoutrements of the rest of the figure is to replot it for the purpose as "ordinary" figure or build the other plot with plotyy instead, saving the two axes handles which you could copyobj
vgloco
vgloco on 21 Aug 2018
Thanks for the response. Sady I can't upload my used GUI-fig. (When I save it with savefig while using the GUI, it has a size of 500mB even when I used the 'compact' flag.)
My idea to solve my problem was to save the whole GUI window as a under a different name as a .fig file. Then run a script which opens the .fig, deletes all ui controls, rearranges the axes and saves then the .fig as emf or png. But it seems that the file is really big (0.5GB) which would slow down the process a lot.
Replotting is bad because the diagram was created by many different functions depending on button clicks in the UI. I would need to save all these little details and the data itself to a .m file. And then create a script which would plot the diagram according to the this data. This is would be really alot of rework considering I just want to save a graph which already exists.
Any other ideas/comments?

Sign in to comment.

Answers (1)

jonas
jonas on 21 Aug 2018
Edited: jonas on 21 Aug 2018
Copyobj doesn't seem to work with yyaxis because you end up with a single axes object having multiple coordinate systems. You can avoid this situation altogether by not using yyaxis. Personally, I prefer this approach over yyaxis:
%%Create double yaxis
ax1=axes('yaxislocation','left');hold on
ax2=axes('yaxislocation','right','xcolor','none');hold on
set([ax1 ax2],'color','none')
linkaxes([ax1 ax2],'x')
%%Call axes and plot like this
axes(ax1)
h1=plot([0 1],[0 1])
axes(ax2)
h2=plot([0 1],[1 0])
%%Copy axes to new figure
figure;
copyobj(ax1,gcf)
copyobj(ax2,gcf)
An alternative would be to hide all UIobjects before saving the figure. However, then you have to 'unhide' after saving the figure.
uis=findobj(gcf,'type','UIControl')
set(uis,'visible','off')
% save...
set(uis,'visible','on')
  12 Comments
vgloco
vgloco on 21 Aug 2018
Hey thanks for all your advice. I know got rid of my yyaxes in my code and replaced it with two axes. Because of background color, current axes, labe, positio, several functions which included plotting on the axes, this took waaay to long. Several hours. Big problem was the legend. Anyway.
I know want to copy the a new invisible axes use the saveas function to .emf. (I need that for word. No other vectorformat allowed in word)
The problem is that when I copy my two axes to the new figure, I also have to copy the legend. copyobj and legend require you to copy legend and the associated axes togehter. no problem. But apparently this stupid copyobj function, only copies the legend entries which are in the associated axes. I have now idea how to solve that.
jonas
jonas on 21 Aug 2018
Edited: jonas on 21 Aug 2018
I understand you've had a rough day with MATLAB today :)
The fix is to copy everything in one go. In my example above, try:
copyobj([ax1 ax2 l],gcf)
where l is the legend handle

Sign in to comment.

Categories

Find more on Printing and Saving 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!