copy axes to clipboard

5 次查看(过去 30 天)
Jason
Jason 2014-11-3
Hello. I am wanting top copy several plots from my gui. One plot is on a uipanel and the other is a graph on an axes component. I have tried the axes component first using :
currAxes = handles.axes1
newFig = figure('visible','off');
newHandle = copyobj(currAxes,newFig);
print(newFig,'-dmeta');
delete(newFig);
It sort of works. The issues are: 1: It only copies one of the yaxis (the plot on axes1 is a plot with two yaxis, using plotyy). 2: It copies with a large box around it.
The attached image shows the result of the copy, and the actual graph I want copied.
Am I using the correct method? Thanks Jason

采纳的回答

Orion
Orion 2014-11-3
if you're using GUIDE, don't forget to get and restore the handles structure with guidata
function Your_plot_Pushbutton(hObject, eventdata, handles)
% code,code,code
[AX,H1,H2] = plotyy(A,B,A,C,'plot');
handles.yyaxis=AX;
% save the changes to the structure
guidata(hObject,handles)
and then you can retrieve it in the copy function
function Your_copy_Pushbutton(hObject, eventdata, handles)
hAX=handles.yyaxis;
  14 个评论
Orion
Orion 2014-11-4
So, gook luck.
And I get one last Idea.
You can try the same callback as in the edit menu of the figure, which seem to work on your machine. It works for me.
Replace the call to print with editmenufcn.
%%metacopy
% print(newFig,'-dmeta');
editmenufcn(newFig,'EditCopyFigure')
Jason
Jason 2014-11-4
Perfect,thankyou again! Jason

请先登录,再进行评论。

更多回答(4 个)

Orion
Orion 2014-11-3
Hi,
Actually, plotyy creates 2 superimposed axes objects (see the doc about the differents outputs arguments).
So you need to copy all the axes in the new fig.
with your code
currAxes = handles.axes1
newFig = figure('visible','off');
newHandle = copyobj(currAxes,newFig);
print(newFig,'-dmeta');
you are only copying one axe, so it's logical that you miss the second.
you may need to do something like:
AllAxes = findall(gcf,'Type','Axes') % get all the axes
newFig = figure('visible','off');
newHandle = copyobj(AllAxes,newFig); % copy all axes in the figure to print
print(newFig,'-dmeta');
  1 个评论
Jason
Jason 2014-11-3
编辑:Jason 2014-11-3
Hi, thankyou for pointing out the behaviour of plotyy.
In your code, you are finding the handles of the type "Axes". The problem with my application is besides this graph, I also have about 5 other axes components that will obviously also be picked up. How do I limit your findall command to just handles.axes1?
This also explains why when I do a cla , only the one plot clears. How can I clear both plots?. Could I use the following but not use delete, instead cla?
delete(get(handles.axes2,'children'))
Thanks Jason

请先登录,再进行评论。


Orion
Orion 2014-11-3
In the prototype of plotyy
[AX,H1,H2] = plotyy(...)
AX is a vector containing the handles of the 2 axes objects created by plotyy.
so when you stock your data, you could do
handles.axesplotyy_1 = AX(1);
handles.axesplotyy_2 = AX(2);
and then use these handles the way you need.
AxesToCopy = [handles.axesplotyy_1 handles.axesplotyy_2];
newFig = figure('visible','off');
newHandle = copyobj(AxesToCopy,newFig);
then do the same to delete/cla
delete([handles.axesplotyy_1 handles.axesplotyy_2]) % destroy both axes
  1 个评论
Jason
Jason 2014-11-3
Hello. Its still not copying all the data, it now has the 2nd y axis labels and tickmarks. Its also still inside a much larger frame thats also being copied, as per original image at top of this thread?

请先登录,再进行评论。


Orion
Orion 2014-11-3
编辑:Orion 2014-11-3
my bad, i went a little fast in my explanation.
So, with an example
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
figure % new figure
[hAx,hLine1,hLine2] = plotyy(x,y1,x,y2);
newFig = figure('visible','on');
% copy the axes in the 2nd figure
hcop = copyobj(hAx,newFig);
axes(hcop(2)); % to put the second axe (transparent) in first plan
% resize if needed the dimensions of the axes to fill screen
% with the value you want (Normalized)
set(findall(newFig,'Type','Axes'),'Position',[ 0.100 0.1100 0.8 0.8150]);
% metacopy
print(newFig,'-dmeta');
  3 个评论
Orion
Orion 2014-11-3
编辑:Orion 2014-11-3
it's really up to you, depending on your "coding style".
Personnaly, I like to use setappdata/getappdata, when manipulating handles between multiple figures.
use setappdata in the plot function
then getappdata in the copy function
Jason
Jason 2014-11-3
编辑:Jason 2014-11-3
hmmmm. I thought I knew how to use the handles structure to hold variables.
This is my plot:
[AX,H1,H2] = plotyy(A,B,A,C,'plot');
set to handles structure so can use outside this callback
handles.yyaxis=AX;
and to retrieve in a different button callback
hAX=handles.yyaxis;
and I get the error: Reference to non-existent field 'yyaxis'.
Error in Montage>pushbutton21_Callback (line 1719)
hAX=handles.yyaxis; %extract axis from handles structure

请先登录,再进行评论。


Orion
Orion 2014-11-3
Matlab is clear,
Undefined function or variable 'hAx'.
when you do
hcop = copyobj(hAx,newFig);
you are using hAx, but you created the variable with hAX : X instead of x.
hourra for the end of the day.

类别

Help CenterFile 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!

Translated by