How do I insert a MATLAB figure into an Excel Workbook through ActiveX?

14 ビュー (過去 30 日間)
I would like to paste a figure into a workbook directly from an MATLAB file.

採用された回答

MathWorks Support Team
MathWorks Support Team 2010 年 2 月 25 日
This change has been incorporated into the documentation in Release 14 Service Pack 3 (R14SP3). For previous releases, read below for any additional information:
You can put a MATLAB figure into Excel using ActiveX. Here is an example that demonstrates two alternatives:
% Create sample image from figure
img = 'figure1.png';
plot(1:10);
print('-dpng', img);
% Get handle to Excel COM Server
Excel = actxserver('Excel.Application');
% Set it to visible
set(Excel,'Visible',1);
% Add a Workbook
Workbooks = Excel.Workbooks;
Workbook = invoke(Workbooks, 'Add');
% Get a handle to Sheets and select Sheet 1
Sheets = Excel.ActiveWorkBook.Sheets;
Sheet1 = get(Sheets, 'Item', 1);
Sheet1.Activate;
% Alternative 1 BEGIN.
% Get a handle to Shapes for Sheet 1
Shapes = Sheet1.Shapes;
% Add image
Shapes.AddPicture([pwd '\' img] ,0,1,400,18,300,235);
% Alternative 1 END.
% Alternative 2 BEGIN.
% Add image
Sheet1.invoke('Pictures').Insert([pwd '\' img]);
% Alternative 2 END.
% Save the workbook and Close Excel
invoke(Workbook, 'SaveAs', [pwd '\myfile.xls']);
invoke(Excel, 'Quit');
In the above code (Alternative 1), Excel's Shapes.AddPicture Method is used to insert a figure in the Excel Sheet. The syntax for AddPicture is:
expression.AddPicture(Filename, LinkToFile, SaveWithDocument, Left, Top, Width, Height), where expression is a variable that represents a Shapes object.
  1 件のコメント
Kevin Gleason
Kevin Gleason 2017 年 4 月 11 日
編集済み: Kevin Gleason 2017 年 4 月 11 日
You can get "Workbook" to be the existing book using the code in the link above, then insert the figure the same way as this answer tells.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeUse COM Objects in MATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by