How do I Copy or Import a figure from MATLAB into Word document
378 views (last 30 days)
Show older comments
Dear Support,
How do I copy or Import a figure (q-q plot graph) from MATLAB into Word document.
Thank you.
3 Comments
Ganta Shanmukha Rao
on 20 May 2021
is it possible to edit the graph in word file after importing from matlab??
Answers (8)
Nafees Ahmad
on 23 Jun 2018
I have searched this question many times, now I got the result by myself.
- # plot the diagram on the figure
- # Then go to the command window of MATLAB and enter
print -dmeta
- Come to the Microsoft word and Paste the result
- Now in Microsoft Word formatting will create a problem. To solve this problem draw a table and paste the Matlab image into Table.
I hope this the answer of this question. Please rate my answer, if you still facing a problem please comment.* Thank you
7 Comments
Image Analyst
on 29 Apr 2020
Here is a full demo that shows you three different ways/options for saving axes into an existing Word Document using Word as an ActiveX server. This demo reads all PNG format images from the drive into the axes, and pastes them into a Word document that you specify:
% Script to save MATLAB axes (containing Tif images, or whatever you want) at the top of a Word Document.
% By Image Analyst. April 2020.
% Initialization steps. Brute force cleanup of everything currently existing to start with a clean slate.
% Just for the demo. If you put this into your own function you'll want to get rid of the close and clear commands.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
%---------------------------------------------------------------------------------------------------------
% Specify the existing Word document that we want to paste images into.
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = pwd; % or 'C:\wherever';
if ~isfolder(startingFolder)
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.doc*');
[baseFileName, wordDocumentFolder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% Exit if the user clicked the Cancel button.
return;
end
wordFullFileName = fullfile(wordDocumentFolder, baseFileName)
if ~isfile(wordFullFileName)
% They probably typed in a new name for a non-existent document.
msgbox('Error: %s must exist already, and it does not.', wordFullFileName);
return;
end
%---------------------------------------------------------------------------------------------------------
% Get a list of TIF images.
imagesFolder = pwd; % The current folder, or wherever you want.
fprintf('Now getting a list of image files in folder "%s".\n', imagesFolder);
filePattern = fullfile(imagesFolder, '*.png'); % Change to whatever extension (image file format) you want.
fileList = dir(filePattern);
if isempty(fileList)
[~, ~, ext] = fileparts(filePattern);
warningMessage = sprintf('No %s format images found in folder\n%s\n\nExiting.', ext, imagesFolder);
uiwait(warndlg(warningMessage));
return; % Exit since there's nothing to do. No images to paste!
end
numberOfFiles = length(fileList);
%---------------------------------------------------------------------------------------------------------
% Open the document in Word using ActiveX.
fprintf('MATLAB is launching Word as an ActiveX server...\n');
Word = actxserver('Word.Application'); % A MATLAB function.
fprintf('Word is now opening "%s" ...\n', wordFullFileName);
Word.Documents.Open(wordFullFileName); % An ActiveX method.
fprintf('Document "%s" has been opened in Word. Now making it visible.\n', wordFullFileName);
Word.Visible = true; % Make it visible. An ActiveX property.
% Set horizontal alignment
% Word.Selection.ParagraphFormat.Alignment options formats:
% 0: Left-aligned
% 1: Center-aligned
% 2: Right-aligned
% 3: Fully justified
% 4: Paragraph characters are distributed to fill the entire
% width of the paragraph
% 5: Justified with a medium character compression ratio
% 6: Does not exist.
% 7: Justified with a high character compression ratio
% 8: Justified with a low character compression ratio
% 9: Justified according to Thai formatting Layout
Word.Selection.ParagraphFormat.Alignment = 0; % Left align the images. An ActiveX property.
hFig = figure; % Bring up new figure.
%---------------------------------------------------------------------------------------------------------
% For each image drop onto the Word document.
for k = 1 : numberOfFiles
pictureFullFileName = fullfile(fileList(k).folder, fileList(k).name);
fprintf('Reading in %s, and pasting onto document.\n', fileList(k).name);
theImage = imread(pictureFullFileName);
imshow(theImage);
hFig.WindowState = 'maximized'; % Need to re-maximize each figure since imshow() unmaximizes them.
caption = sprintf('#%d of %d : %s', k, numberOfFiles, fileList(k).name);
title(caption, 'FontSize', 15, 'Interpreter', 'none'); % Using 'Interpreter', 'none' so underlines in the file name don't cause the title to be subscripted after the underline
drawnow;
pause(0.6); % Optional : Delay a bit so we can see the image before it vanishes.
% Pick only ONE of the 3 options below to paste the axes into Word at the beginning of the document.
%---------------------------------------------------------------------------------------------------------
% OPTION 1: Transfer to Word by copying the current axes object to the clipboard and pasting into Word using ActiveX method Selection.Paste().
% If you're using r2020a or later, use copygraphics() instead of the print() to copy the figure to the clipboard.
copygraphics(gca); % A MATLAB function. This will also copy the title above the image, and the axis tick marks (if showing) in addition to the image.
Word.Selection.Paste; % An ActiveX method.
%---------------------------------------------------------------------------------------------------------
% OPTION 2: Transfer to Word by giving the file name string to the InlineShapes.AddPicture method of ActiveX.
% Tell Word to read the file from the drive using it's filename, and then insert into the document.
% Word.Selection.InlineShapes.AddPicture(pictureFullFileName,0,1); % An ActiveX method.
%---------------------------------------------------------------------------------------------------------
% OPTION 3: Transfer to Word by giving the file name string to the InlineShapes.AddPicture method of ActiveX.
% Pre-2020a, use print to copy the figure to the clipboard.
% print(gca, '-dmeta'); % Export figure to clipboard
% invoke(Word.Selection,'Paste'); % An ActiveX method.
end
%---------------------------------------------------------------------------------------------------------
% Clean up and shutdown.
% Close the image figure window.
close(hFig);
% Save the active Word document without prompting the user.
fprintf('Now saving Word document "%s".\n', wordFullFileName);
Word.ActiveDocument.Save(); % An ActiveX method.
% Shut down Word. Close it.
fprintf('Now shutting down the Word ActiveX server.\n');
Word.Quit; % An ActiveX method.
% Clear the variable from MATLAB's memory (workspace).
clear('Word');
% Let user know that we're all done, and offer to open the file.
promptMessage = sprintf('Done pasting %d images into Word.\nDo you want to open the document in Word?', numberOfFiles);
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Yes - Continue', 'No - Quit', 'Yes - Continue');
if contains(buttonText, 'Yes', 'IgnoreCase', true)
% Cause the operating system to launch Word and open the document.
winopen(wordFullFileName);
end
fprintf('Done running %s.m ...\n', mfilename);
Mithun Mahto
on 5 Jan 2018
- MATLAB Figure window: Edit -> Copy Figure
- Switch to Word and paste (ctrl + v)
0 Comments
Shashank Prasanna
on 5 May 2013
Put your code in a script and publish it.
You can publish either by clicking on the publish button in the editor or running the publish command on the commandline. Change the output format to Word, powerpoint, html or any other format you desire.
2 Comments
Shashank Prasanna
on 6 May 2013
Edited: Shashank Prasanna
on 6 May 2013
That is one way, but if you don't mind copying and pasting into word (instead of the automated may i pointed out earlier) The better approach is to click on File menu > Edit > Copy Figure
This will copy just the figure as displayed, which you can then paste in the Word document.
Azzi Abdelmalek
on 4 May 2013
Save your figure as jpg format
h=plot(t,y)
saveas(h,'filename.jpg')
3 Comments
Image Analyst
on 5 Mar 2021
Not a good idea. You should use PNG format, not JPG to save. JPG introduces compression artifacts whereas PNG does not.
John
on 27 Jun 2015
I would just like to add to Azzi's answer above,
h=plot(t,y)
saveas(h,'filename.jpg')
but also share that for me, jpg's in Word docs, show a lot of artifacts that make the figure look almost fuzzy or out of focus. I have had good luck with png or pdf in Word docs.
saveas(h,'filename.png')
saveas(h,'filename.pdf')
0 Comments
Shar Timman
on 16 Jan 2020
Select the plots you've generated, right-click and "Create Zip File", then you'll find the Download function under HOME is activated and you can save it to your files.
0 Comments
Image Analyst
on 26 Apr 2020
If you want to do it via ActiveX in Windows, you can save the image to disk (temporarily of you want), then you can bring up Word as an active X server.
0 Comments
See Also
Categories
Find more on Live Scripts and Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!