MATLAB: How to automatically export an image to a Microsoft Word file without using the PUBLISH function in MATLAB 7.10 (R2010a)

MATLAB

I want to export an image file to a Word file without using the PUBLISH function or manually copying and pasting the figure.

Best Answer

You can work with the Word Object Model in MATLAB to add an image to a Word file. For example, suppose you have an image 'test_img.png'. The following script will look for this image in the current working directory and export the image to a word file, 'mydoc.doc' in the current working directory:
imgName =[pwd, '\test_img.png'];
fileName = [pwd, '\mydoc.doc'];
wordApplication=actxserver('Word.Application');
% Define constants.
wdFormatDocument = 0;
%set(wordApplication,'Visible',1); % Great for debugging.
documents = wordApplication.Documents;
% Create a new document.
documents.Add;
doc = documents.Item(documents.Count);
selection = wordApplication.Selection;
% add picture
selection.InlineShapes.AddPicture(imgName); % absolute path to the image
selection.TypeParagraph;
selection.TypeParagraph;
% save and close
doc.SaveAs(fileName,wdFormatDocument);
doc.Close(0);
wordApplication.Quit
For more information on Word Object Model, please refer the following webpage:
Note that the above script requires Microsoft Word installed on the system.