Print and resize MATLAB figure in Excel
I have two figures in MATLAB with the handles hFig1
and hFig2
. I would like to print them to specific cells in Excel (cells E3 and I3) and reshape them to each be [2in x 3in].
I have tried using the .AddPictures
object handler and using print -dmeta
, but I can't find a wa开发者_开发知识库y to achieve all three of my objectives.
I am also writing data to excel at the same time and because there is a lot of data lines being sent, I was hoping to have a method that didn't require continually invoking excel with the ActiveXServer.
Does anyone have a good method or resource for this kind of problem?
You can add MATLAB figures to Excel most easily like this:
% Create some arbitrary graphics
f1 = figure; peaks; f2 = figure; membrane;
% Connect to Excel, make it visible and add a worksheet
xl = actxserver('Excel.Application'); set(xl,'Visible',1);
xl.Workbooks.Add(1); xls = xl.ActiveSheet;
% Paste in the MATLAB figures
print(f1, '-dbitmap'); xls.Range('E3').PasteSpecial;
print(f2, '-dbitmap'); xls.Range('I3').PasteSpecial;
I'm not sure exactly what you mean by making the images 2in by 3in. Is that the size on screen? At what resolution? Or is it the size when printed?
You can specify the size and position of the image in points directly:
xls.Shapes.Item(1).PictureFormat.CropLeft = 30;
xls.Shapes.Item(1).PictureFormat.CropRight = 30;
xls.Shapes.Item(1).Height = 200;
xls.Shapes.Item(1).Left = xls.Range('E3').Left;
But if you want it in inches you'll need a way of converting to points, depending on the way it's displayed.
You mention that you're continually invoking Excel with actxserver
; are you connecting over and over again each time you write each piece of data? You probably don't need to do that - more likely you could keep a single connection open and write each piece of data to it, then save the file and close the connection.
A good reference resource for the Excel Object Model is the Microsoft documentation. It's not an easy read though :)
精彩评论