winggundam
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Problem copying Matlab figure into Word

向下

Problem copying Matlab figure into Word Empty Problem copying Matlab figure into Word

發表 由 lung 周二 11月 22, 2011 4:53 pm

lung
lung
Admin

文章數 : 26067
注冊日期 : 2009-07-12

回頂端 向下

Problem copying Matlab figure into Word Empty 回復: Problem copying Matlab figure into Word

發表 由 lung 周二 11月 22, 2011 4:53 pm

I have having difficulty automating the process of copying
Matlab-generated figures into Word. Manually it works fine when in
the Figure menu I select 'Copy Figure' and then paste into Word.
Doing this gets me a nice high-quality copy of the figure in Word.
The problem though is when I try to automate it. When I automate it
using the lines of code shown below, I get a lower quality copy of the
figure where the text on the figure is very difficult to read.

Here are the lines of code I am using:

% Generate data and plot
for i=1:100
y(i) = log(i);
end
h1 = figure; plot (y,i);
text(4.9,10,'text1 = 0.9999','HorizontalAlignment','right');

% Open link to Word
word = actxserver('Word.Application');

% Create new Word document
op = invoke(word.Documents,'Add');

% Find end of document and make it the insertion point
end_of_doc = get(word.activedocument.content,'end');
set(word.application.selection,'Start',end_of_doc);
set(word.application.selection,'End',end_of_doc);

% Copy figure to Clipboard
print(h1,'-dmeta');

% Paste figure to Word document (use PasteSpecial instead of Paste
% because I want to disable 'Float Over Text')
invoke(word.Selection,'PasteSpecial',0,0,0,0,3);

% Save file
filename = 'C:\temp\temp.doc';
invoke(op,'SaveAs',filename,1);

% Close application window
invoke(op,'Close');

% Quit MS Word
invoke(word,'Quit');

Does anyone know how I can modify this code to get a better quality
copy of the figure into Word? If I could get a copy of the figure
that looks like the copy I get when manually selecting ‘Copy Figure'
from the Figure window, that would be ideal.
lung
lung
Admin

文章數 : 26067
注冊日期 : 2009-07-12

回頂端 向下

Problem copying Matlab figure into Word Empty 回復: Problem copying Matlab figure into Word

發表 由 lung 周二 11月 22, 2011 4:53 pm

> % Copy figure to Clipboard
> print(h1,'-dmeta');

a hint:

print(h1,'-dmeta','-r300');

you might want to adjust the <rx> to your needs...
lung
lung
Admin

文章數 : 26067
注冊日期 : 2009-07-12

回頂端 向下

Problem copying Matlab figure into Word Empty 回復: Problem copying Matlab figure into Word

發表 由 lung 周四 11月 24, 2011 5:35 pm

word = actxserver('Word.Application');

word.Visible = 1;

op = invoke(word.Documents,'Add');

active=word.ActiveDocument;

%% Table 1
% Create 4x4 table
range=word.Selection.Range;
shapes=word.Selection.InlineShapes;
t=invoke(word.ActiveDocument.Tables,'add',range,2,2);

% fill each cell with an image, image will fit to table
invoke(shapes,'addpicture',fullfile(pwd, 'fig1.wmf'));

invoke(word.Selection,'moveright',1,1);
invoke(shapes,'addpicture',fullfile(pwd, 'fig2.wmf'));

invoke(word.Selection,'moveright',1,2);
invoke(shapes,'addpicture',fullfile(pwd, 'fig3.wmf'));

invoke(word.Selection,'moveright',1,1);
invoke(shapes,'addpicture',fullfile(pwd, 'fig4.wmf'));

%% Table 2, I want this on a new page
invoke(word.Selection,'InsertBreak',2); % how do I get the next stuff on a new page?
range=word.Selection.Range;
shapes=word.Selection.InlineShapes;
t=invoke(word.ActiveDocument.Tables,'add',range,1,2);

invoke(shapes,'addpicture',fullfile(pwd, 'fig1.wmf'));
invoke(word.Selection,'moveright',1,1);
invoke(shapes,'addpicture',fullfile(pwd, 'fig2.wmf'));


http://stackoverflow.com/questions/5467769/how-do-i-insert-multiple-figures-into-multiple-tables-in-matlab
lung
lung
Admin

文章數 : 26067
注冊日期 : 2009-07-12

回頂端 向下

Problem copying Matlab figure into Word Empty 回復: Problem copying Matlab figure into Word

發表 由 lung 周四 11月 24, 2011 6:38 pm

img = imread('figure.jpg');
read figure into Matlab
lung
lung
Admin

文章數 : 26067
注冊日期 : 2009-07-12

回頂端 向下

回頂端


 
這個論壇的權限:
無法 在這個版面回復文章