- I'm using the field width option to make each 'column' 6 characters wide
- Use Courier New as your font so that all characters are the same width.
Copy and Paste Matrix into Word
116 views (last 30 days)
Show older comments
Jucimar Carpe
on 28 Aug 2019
Commented: Cris LaPierre
on 30 Apr 2022
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
Accepted Answer
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
Here's what this output looks like.
5 Comments
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);
More Answers (1)
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:
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.
2 Comments
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
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.
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!