How can I save an axes which has multiple coordinate systems?
이전 댓글 표시
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
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
2018년 8월 21일
답변 (1개)
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
vgloco
2018년 8월 21일
vgloco
2018년 8월 21일
jonas
2018년 8월 21일
Yea I already updated my solution to include that! Works like a charm. Will see if I can find a solution to "crop" the image.
As a sidenote, I do not think it will take so much work to change from yyaxis to double axes. Basically you just create the two axes at the start of the script and then find/replace:
- yyaxis left -> axes(ax1)
- yyaxis right -> axes(ax2)
May be some other settings that I am missing, but the two methods are very similar.
jonas
2018년 8월 21일
export_fig crops the image per default (including invisible objects). I cannot recommend this function enough.
vgloco
2018년 8월 21일
It works for me. This simple code works like a charm.
axes
yyaxis right
export_fig(1,'-png','test')
What is the error message? Do you have the latest version?
vgloco
2018년 8월 21일
To be honest I did not know you could save an axes with export_fig! Probably it just copies your axes to another figure and saves it, just like you wanted to do initially.
Why can't you just save the entire figure though? I thought that was your intention? In your own example, the image is perfectly cropped after exporting.
vgloco
2018년 8월 21일
"Use the set and get functions to access properties for an array of objects."
uiCon is an array of objects, i.e. multiple objects. It's therefore best to use
set(uiCon,'visible','off')
PS: don't forget to accept if you found the answer helpful ;)
vgloco
2018년 8월 21일
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
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!