How do I Copy or Import a figure from MATLAB into Word document
조회 수: 393 (최근 30일)
이전 댓글 표시
Dear Support,
How do I copy or Import a figure (q-q plot graph) from MATLAB into Word document.
Thank you.
댓글 수: 3
Ganta Shanmukha Rao
2021년 5월 20일
is it possible to edit the graph in word file after importing from matlab??
답변 (8개)
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
댓글 수: 7
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);
Mithun Mahto
2018년 1월 5일
- MATLAB Figure window: Edit -> Copy Figure
- Switch to Word and paste (ctrl + v)
댓글 수: 0
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
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
2013년 5월 4일
Save your figure as jpg format
h=plot(t,y)
saveas(h,'filename.jpg')
댓글 수: 3
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
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')
댓글 수: 0
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.
댓글 수: 0
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Live Scripts and Functions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!