How do I Copy or Import a figure from MATLAB into Word document

779 ビュー (過去 30 日間)
Silas Adiko
Silas Adiko 2013 年 5 月 4 日
コメント済み: Anna Antoniou 2022 年 3 月 14 日
Dear Support,
How do I copy or Import a figure (q-q plot graph) from MATLAB into Word document.
Thank you.
  2 件のコメント
Ganta Shanmukha Rao
Ganta Shanmukha Rao 2021 年 5 月 20 日
is it possible to edit the graph in word file after importing from matlab??

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

回答 (8 件)

Nafees  Ahmad
Nafees Ahmad 2018 年 6 月 23 日
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
  4 件のコメント
Anna Antoniou
Anna Antoniou 2022 年 3 月 14 日
Thank you

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


Image Analyst
Image Analyst 2020 年 4 月 29 日
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);
  1 件のコメント
Ben McMahon
Ben McMahon 2021 年 8 月 19 日
Yup thats a good script. V helpful. Thanks

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


Mithun Mahto
Mithun Mahto 2018 年 1 月 5 日
  • MATLAB Figure window: Edit -> Copy Figure
  • Switch to Word and paste (ctrl + v)

Shashank Prasanna
Shashank Prasanna 2013 年 5 月 5 日
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 件のコメント
Shashank Prasanna
Shashank Prasanna 2013 年 5 月 6 日
編集済み: Shashank Prasanna 2013 年 5 月 6 日
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
Azzi Abdelmalek 2013 年 5 月 4 日
Save your figure as jpg format
h=plot(t,y)
saveas(h,'filename.jpg')
  3 件のコメント
Image Analyst
Image Analyst 2021 年 3 月 5 日
Not a good idea. You should use PNG format, not JPG to save. JPG introduces compression artifacts whereas PNG does not.

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


John
John 2015 年 6 月 27 日
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')

Shar Timman
Shar Timman 2020 年 1 月 16 日
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.

Image Analyst
Image Analyst 2020 年 4 月 26 日
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.

Community Treasure Hunt

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

Start Hunting!

Translated by