Copy and Paste Matrix into Word

116 views (last 30 days)
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));

Accepted Answer

Cris LaPierre
Cris LaPierre on 29 Aug 2019
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 Comments
Cris LaPierre
Cris LaPierre on 29 Aug 2019
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 on 29 Aug 2019
Woww, it worked perfect! Thank you very much!!!!
I've been working on this for 2 days and now it's working...

Sign in to comment.

More Answers (1)

Cris LaPierre
Cris LaPierre on 29 Aug 2019
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 Comments
Said Aldughaishi
Said Aldughaishi on 30 Apr 2022
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 on 30 Apr 2022
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.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!