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

jonas
jonas 2018년 8월 19일
편집: jonas 2018년 8월 19일
Could you upload a sample .fig with some UI objects (or code)?
dpb
dpb 2018년 8월 19일
편집: dpb 2018년 8월 19일
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
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?

댓글을 달려면 로그인하십시오.

답변 (1개)

jonas
jonas 2018년 8월 21일
편집: jonas 2018년 8월 21일
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

My program is quite big already so changing now the yyaxis to two overlapping axis would be quite a lot of work.
I found another trick: when i want to save the axes, I set the visibility of the uipanels to 'off' and then after saving is done back to 'on'. The problem is now, that the created .emf and .png has alot of white space around the axes. (The place where uicontrols are (invisible) located)
Nice. You had the same idea as me! :)
Do you know how to remove the white space which will be at the location s where the UI controls were?
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.
export_fig crops the image per default (including invisible objects). I cannot recommend this function enough.
export_fig doesn't work with two yyaxes ;)
jonas
jonas 2018년 8월 21일
편집: jonas 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?
I wanted to use the export_fig to just print my axes, not the figure:
>> f1 = figure(1);
yyaxis right
plot(rand(5,2));
yyaxis left
plot(rand(5,2)*10);
ax= gca;
export_fig(ax,'Test');
Error using copyobj
Object Copy of Axes with multiple coordinate systems is not supported.
Error in copyfig (line 30) fh = copyobj(fh, 0);
Error in isolate_axes (line 56) fh = copyfig(fh); %copyobj(fh, 0);
Error in export_fig (line 296) fig = isolate_axes(fig);
When I use
export_fig(f1,'Test') it works. So, I will look into other ways how to crop the figure with export_fig.
jonas
jonas 2018년 8월 21일
편집: jonas 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.
Yes I was just explaining why I thought it doesnt work. I am doing it now as you suggested and it crops nicely!
To make the uiobjects invisble, I used this method:
uiCon =intersect(findobj('Visible','on','-not','Type','Axes','-not','Type','Legend'),allchild(handles.MyFigure));
for k = 1:length(uiCon)
uiCon(k).Visible = 'off';
end
export_fig(handles.MyFigure,'myplot.png');
and then turn it the visibilty of uiCon back on.
To my confusion the following doesnt work:
[uiCon(:).Visible] = deal('off')
No appropriate method, property, or field 'Visible' for class 'matlab.ui.control.WebComponent'.
Any idea why? The for-loop works though.
jonas
jonas 2018년 8월 21일
편집: jonas 2018년 8월 21일
From the doc on "dot notation" vs "set" ( link )
"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 ;)
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 2018년 8월 21일
편집: jonas 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에 대해 자세히 알아보기

질문:

2018년 8월 19일

편집:

2018년 8월 21일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by