필터 지우기
필터 지우기

Copy and Paste Matrix into Word

조회 수: 130 (최근 30일)
Jucimar Carpe
Jucimar Carpe 2019년 8월 28일
댓글: Cris LaPierre 2022년 4월 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