Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Having Trouble Exporting Plot Image To Excel

41 views
Skip to first unread message

Jeff

unread,
Nov 4, 2010, 10:24:04 PM11/4/10
to
I am working on a project where I need to export an image (jpeg) of a plot figure to an excel spreadsheet. I can send number data over but am unable to get the image to go over. Here's my code.

%Create a graph
x = 0:.1:2*pi;
y = sin(x);
f = plot(x,y);
print -djpeg -f thefigfile

%Start Excel
excl = actxserver('excel.application');
set(excl,'visible',1);
wkbk = excl.Workbooks;
exclFile = wkbk.Open('C:\DataToExcel.xls');
rptSheet = exclFile.Sheets.Item('Sheet1');
imgrng = rptSheet.Range('I20')
imgrng.Select

%AddPicture(shps, 'thefigfile.jpg')
shps = rptSheet.Shapes;
shps.AddPicture('thefigfile.jpg');

%Quit Excel
excl.Quit

The error I get is "??? No method 'AddPicture' with matching signature found for class 'Interface.Microsoft_Excel_12.0_Object_Library.Shapes'."

Error in ==> SendToExcel at 18
shps.AddPicture('thefigfile.jpg');

Help>
Jeff

ImageAnalyst

unread,
Nov 4, 2010, 10:47:15 PM11/4/10
to
Jeff
Apparently it thinks AddPicture is not one of the methods you can use
for the shapes object - it doesn't have that method. Did you find it
here?
http://msdn.microsoft.com/en-us/library/bb979621%28v=office.12%29.aspx

However here it seems to say that it should work.
http://msdn.microsoft.com/en-us/library/bb209605.aspx
I don't have an answer but the answer is probably buried in that MS
documentation somewhere, although it could take a while to find
because the documentation is so deep and covers so many versions.

Jeff

unread,
Nov 4, 2010, 11:12:03 PM11/4/10
to
Actually, I got most of the code directly from MatLab's Help/User Guide on COM. Here's the code from MatLab.

exl2 = actxserver('excel.application');
exlWkbk2 = exl2.Workbooks;
wb = invoke(exlWkbk2,'Add');
graphSheet = invoke(wb.Sheets,'Add');
Shapes = graphSheet.Shapes;

function saveButtonCallback(src,evt)
tempfig = figure('Visible','off','PaperPositionMode','auto');
tempfigfile = [tempname '.png'];
ah = findobj(f,'type','axes');
copyobj(ah,tempfig) % Copy both graph axes and legend axes
print(tempfig,'-dpng',tempfigfile);
Shapes.AddPicture(tempfigfile,0,1,50,18,300,235);
exl2.visible = 1;
end

I did change a couple bits of the code, but nothing that I thought would change the functionality. I guess I can run the entire example and see what happens. Just not sure why this isn't working.

Steven_Lord

unread,
Nov 5, 2010, 9:41:22 AM11/5/10
to

"Jeff " <jag...@yahoo.com> wrote in message
news:iavsm3$88r$1...@fred.mathworks.com...

The big change I can see that you made that WOULD change the functionality:

%AddPicture(shps, 'thefigfile.jpg')
shps = rptSheet.Shapes;
shps.AddPicture('thefigfile.jpg');

Note that in the code from the User Guide, the AddPicture method is being
called with seven input arguments. It's entirely possible (I'm not familiar
enough with Microsoft Excel's COM interface to know for certain) that
AddPicture _requires_ all seven inputs. Check the documentation on
Microsoft's website to which ImageAnalyst directed you to determine what
those inputs represent and how many of them are required.

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Jeff

unread,
Nov 5, 2010, 10:17:04 AM11/5/10
to
Steve,

Thanks for the input. I did try using the same 7 values - albeit with no idea od what they represent. I'm guilty of not seriously looking at the definition of those parameters. I guess I'll have a look and see if I can figure (no punintended) something out.

Jeff

Jeff

unread,
Nov 5, 2010, 11:48:04 AM11/5/10
to
Just thinking about this problem. I guess I could simply link the image in excel to a "tempfile.jpeg" image file. That way I could grab the plot, save it to "tempfile.jpeg", then open excel and the new image would appear without actively placing the image inside of excel.

Jeff

0 new messages