copy graphic to Excel

조회 수: 5 (최근 30일)
Jason
Jason 2011년 5월 13일
How can I take the code below to just copy the graphic on axes component (axes1) to clipboard. I then have code to paste it to excel. It currently copies all of my matlab screen.
axes(handles.axes1)
Z=peaks
surf(Z)
print -dmeta; %.................Copying to clipboard

답변 (2개)

Andy
Andy 2011년 5월 13일
screencapture('handle',ax, 'filename', fn); % save the graph in file fn
exlSheet.Shapes.AddPicture(fn,0,1,...
20,... % distance from left of sheet
20,... % distance from top of sheet
600,... % width
400); % height
Note that you will need to have opened an actxserver connection to MATLAB. In the above, ax is the handle to your axes, fn is the file where your image is saved, and exlSheet is an Excel Sheet object.
EDIT: Here is some sample actxserve code. I have had a chance to test this and it works for me. Refer to http://www.mathworks.com/help/techdoc/ref/actxserver.html if needed.
x = 0:0.1:10;
y = sin(x);
f = figure;
ax = axes('parent',f);
plot(ax,x,y);
fn = fullfile(pwd,'myplot.bmp');
screencapture('handle',ax, 'filename', fn);
e = actxserver('Excel.application');
eW = e.Workbooks;
eF = eW.Add; % start a new file; use Open method for existing files
eS = eF.ActiveSheet;
e.Visible = 1; % Excel window won't show up by default
eS.Shapes.AddPicture(fn,0,1,...
20,... % distance from left of sheet
20,... % distance from top of sheet
400,... % width
300); % height
  댓글 수: 17
Jason
Jason 2011년 5월 16일
Im, still confused over the figure1 issue. On my GUI created in GUIDE, I drag an axes component and leave its tag as default - axes1. I then plot to axes 1 by :
axes(handles.axes1)
Z=peaks;
surf(Z);
I do not create a figure.
So writing hf=handles.figure1 and then print(handles.figure1, '-dmeta');
just copys the whole GUI to clipboard, not the axes 1 component that I am after. Is it worth sharing my files?
Thanks
Andy
Andy 2011년 5월 16일
That's correct. It's a documented limitation of the print function that it cannot take as an argument, for example, just the axes handle. Yair Altman's screencapture utility that I linked to before CAN capture only the axes without copying the whole figure. I'm not at MATLAB right now, so I can't be certain, but I don't think there is a way for the screencapture utility to write to the system clipboard. That's why I suggested writing to a file and then importing the file into Excel. I really think this is the best solution, but when I'm next using MATLAB I can try to think of something that doesn't produce the intermediate file.

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


Laura Proctor
Laura Proctor 2011년 5월 13일
hf = figure;
surf(peaks)
print('-dmeta',hf)
  댓글 수: 6
Jason
Jason 2011년 5월 17일
It still copies the whole GUI and not just the graphic in the axes component.
Andy
Andy 2011년 5월 17일
Yes, that has been pointed out to you before. I have updated the code in my answer with tested code which works correctly and copies only the axes.

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

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by