Copy and Paste Matrix into Word

106 次查看(过去 30 天)
Hi,
I'm trying to copy and paste into MS Word a series of results from my code. I've been doing this so long but now i need to copy and paste a matrix(5,5) and i couldn't find a way to do it so. I'd like to paste and show in the same way Matlab shows the results on screen.
Can someone help me out?
Below the code i've been working.
word = actxserver('Word.Application'); %start Word
word.Visible =1; %make Word Visible
document=word.Documents.Add; %create new Document
selection=word.Selection; %set Cursor
selection.Font.Name='Arial'; %set Font
selection.Font.Size=9; %set Size
selection.Pagesetup.RightMargin=28.34646; %set right Margin to 1cm
selection.Pagesetup.LeftMargin=28.34646; %set left Margin to 1cm
selection.Pagesetup.TopMargin=28.34646; %set top Margin to 1cm
selection.Pagesetup.BottomMargin=28.34646; %set bottom Margin to 1cm
%1cm is circa 28.34646 points
selection.Paragraphs.LineUnitAfter=0.01; %sets the amount of spacing
%between paragraphs(in gridlines)
selection.TypeText(escolha_lista_curto_word); %write Text
selection.TypeParagraph; %linebreak
selection.TypeParagraph; %linebreak
selection.TypeText('Y_barra(+) = ');
selection.TypeParagraph; %linebreak
selection.TypeParagraph; %linebreak
selection.TypeText(mat2str(ybarra_1,3));

采纳的回答

Cris LaPierre
Cris LaPierre 2019-8-29
Your code for doing this programmatically is close. I'd suggest using sprintf to create the formatted strings, then write that to word. Here's an example
% Data to write to word
escolha_lista_curto_word = "Sample text"
ybarra_1 = magic(5);
%% open the doc
word = actxserver('Word.Application'); %start Word
word.Visible =1; %make Word Visible
document=word.Documents.Add; %create new Document
selection=word.Selection; %set Cursor
selection.Pagesetup.RightMargin=28.34646; %set right Margin to 1cm
selection.Pagesetup.LeftMargin=28.34646; %set left Margin to 1cm
selection.Pagesetup.TopMargin=28.34646; %set top Margin to 1cm
selection.Pagesetup.BottomMargin=28.34646; %set bottom Margin to 1cm
%1cm is circa 28.34646 points
selection.Paragraphs.LineUnitAfter=0.01; %sets the amount of spacing
%between paragraphs(in gridlines)
selection.TypeText(escolha_lista_curto_word); %write Text
selection.TypeParagraph; %linebreak
selection.TypeParagraph; %linebreak
% Write variable name
selection.Font.Name='Consolas'; %set Font
selection.Font.Size=9; %set Size
selection.TypeText('Y_barra(+) = ');
% Write variable size in gray
selection.Font.ColorIndex = 16;
str = sprintf('%d%c%d\n',size(ybarra_1,1),'x',size(ybarra_1,1));
selection.TypeText(str);
% Write variable values
selection.Font.ColorIndex = 1;
selection.Font.Name='courier New'; %set Font
selection.Font.Size=10; %set Size
str = sprintf([repmat('%6g',1,size(ybarra_1,2)) '\n'],ybarra_1);
selection.TypeText(str);
% delete server object
delete(word);
A couple things to point out
  1. I'm using the field width option to make each 'column' 6 characters wide
  2. Use Courier New as your font so that all characters are the same width.
Here's what this output looks like.
Capture.PNG
  5 个评论
Cris LaPierre
Cris LaPierre 2019-8-29
Try something like this:
ybarra_1Rot = ybarra_1.';
str = sprintf([repmat('%9.4f + %6.4fi',1,size(ybarra_1,2)) '\n'],[real(ybarra_1Rot(:)),imag(ybarra_1Rot(:))]');
selection.TypeText(str);
Jucimar Carpe
Jucimar Carpe 2019-8-29
Woww, it worked perfect! Thank you very much!!!!
I've been working on this for 2 days and now it's working...

请先登录,再进行评论。

更多回答(1 个)

Cris LaPierre
Cris LaPierre 2019-8-29
There is a quick and easy way to get it to Word. If you are using a live script, from the Live Editor tab select Save > Export to Word. For example, if my code were
magic(5)
When I export to Word, I see this:
Capture.PNG
Now that it's in Word, I can do whatever I want with it. If you don't want to include code, select the Hide Code option on the View tab before publishing.
You can read a little more about it here.
  2 个评论
Said Aldughaishi
Said Aldughaishi 2022-4-30
I used this way, but it doesn't write the whole matrix. my matrix size is 12x14, it wrote 7 columns only.
Cris LaPierre
Cris LaPierre 2022-4-30
Yes, that is a limitation of this approach. You many need to look into a more programmatic way of transferring the matrix to work, such as what is shown in the accepted answer.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by