MATLAB Answers

0

Copy and Paste Matrix into Word

Asked by Jucimar Carpe on 28 Aug 2019
Latest activity Commented on by Jucimar Carpe on 29 Aug 2019
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));

  0 Comments

Sign in to comment.

Products


Release

R2018a

2 Answers

Answer by Cris LaPierre
on 29 Aug 2019
 Accepted Answer

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

Hi Cris, yes, indeed it seems to be the formatSpec and now it showed in the way it should be.
The problem is that still not showing the numbers tored in ybarra_1. I think i should use the fprintf command with repmat but i don't know how to do it.
Can you help me?
str = sprintf([repmat('%f%+fi ',1,size(ybarra_1,2)) ' \n'],ybarra_1);
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);
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.


Answer by 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.

  0 Comments

Sign in to comment.