function Ceshi_excel
% setting test Excel file name and path
Filespec_user=[pwd ' \ Test. xls '];
% to determine if Excel is open, and if it is already open, work in open Excel.
% otherwise open Excel
Try
Excel=actxgetrunningserver (' Excel.Application ');
Catch
Excel = Actxserver (' Excel.Application ');
End
% set Excel property to visible
Set (Excel, ' Visible ', 1);
% return Excel workbook handle
Workbooks = Excel.Workbooks;
% if the test file exists, open the test file, otherwise, create a new workbook and save the file name as test. Excel
If exist (filespec_user, ' file ');
Workbook = Invoke (Workbooks, ' Open ', filespec_user);
Else
Workbook = Invoke (Workbooks, ' Add ');
Workbook.saveas (Filespec_user);
End
% return sheet handle
Sheets = Excel.ActiveWorkBook.Sheets;
% returns the first table handle
Sheet1 = Get (Sheets, ' Item ', 1);
% activates the first table
Invoke (Sheet1, ' Activate ');
% if there is a graphic present in the current worksheet, remove the graph by looping all
Shapes=excel.activesheet.shapes;
if shapes.count~=0;
For I=1:shapes.count;
Shapes.item (1). Delete;
End
End
% randomly produces a standard normal distribution random number, draws a histogram, and sets the graphic properties
Zft=figure (' units ', ' normalized ', ' position ',...
[0.280469 0.553385 0.428906 0.251302], ' visible ', ' off ');
Set (GCA, ' position ', [0.1 0.2 0.85 0.75]);
Data=normrnd (0,1,1000,1);
hist (data);
Grid on;
Xlabel (' test results ');
Ylabel (' number ');
% copy the graphic to the Pasteboard
Hgexport (zft, '-clipboard ');
% paste the graphic into the A5:b5 bar of the current table
Excel.ActiveSheet.Range (' a5:b5 '). Select;
Excel.ActiveSheet.Paste;
% Delete Graphics handle
Delete (zft);
2. Use MATLAB to generate a Word document with pictures
function Ceshi_word
% setting test Word file name and path
Filespec_user=[pwd ' \ Test. doc '];
% to determine if Word is open, and if it is already open, work in the open Word.
% otherwise open Word
Try
Word = Actxgetrunningserver (' Word.Application ');
Catch
Word = Actxserver (' Word.Application ');
End
% set Word property to visible
Set (Word, ' Visible ', 1);
% return Word file handle
Documents = word.documents;
% if the test file exists, open the test file, otherwise, create a new file and save the file name as test. doc
If exist (filespec_user, ' file ');
Document = Invoke (documents, ' Open ', filespec_user);
Else
Document = Invoke (documents, ' ADD ');
Document. SaveAs (Filespec_user);
End
Content = document. Content;
selection = Word.selection;
ParagraphFormat = Selection. ParagraphFormat;
% Page Setup
Document. Pagesetup.topmargin = 60;
Document. Pagesetup.bottommargin = 45;
Document. PageSetup.LeftMargin = 45;
Document. Pagesetup.rightmargin = 45;
% set Content start position and title
Set (content, ' Start ', 0);
title= ' test file ';
Set (content, ' Text ', title);
Set (ParagraphFormat, ' Alignment ', ' wdalignparagraphcenter ');
% Set Title font format
Rr=document. Range (0,10);
Rr. font.size=16;
Rr. font.bold=4;
% sets the starting position of the following content
End_of_doc = Get (content, ' end ');
Set (Selection, ' Start ', end_of_doc);
% another paragraph
Selection. TypeParagraph;
% if there is a graphic present in the working document, remove the graph by looping all
Shape=document. Shapes;
Shape_count=shape. Count;
if shape_count~=0;
For I=1:shape_count;
Shape. Item (1). Delete;
End
End
% randomly produces a standard normal distribution random number, draws a histogram, and sets the graphic properties
Zft=figure (' units ', ' normalized ', ' position ',...
[0.280469 0.553385 0.428906 0.251302], ' visible ', ' off ');
Set (GCA, ' position ', [0.1 0.2 0.85 0.75]);
Data=normrnd (0,1,1000,1);
hist (data);
Grid on;
Xlabel (' test results ');
Ylabel (' number ');
% copy the graphic to the Pasteboard
Hgexport (zft, '-clipboard ');
% paste the graphic into the current document and set the graphic property to float above the text
Selection. range.pastespecial;
Shape. Item (1). wrapformat.type=3;
Shape. Item (1). ZOrder (' msoBringInFrontOfText ');
% Delete Graphics handle
Delete (zft);
Matlab generate images, export directly to Excel, Word (reprint)