How can I save an axes which has multiple coordinate systems?
50 views (last 30 days)
Show older comments
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
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
Answers (1)
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
See Also
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!