How do I insert the output of a command as formatted text in a live script.

조회 수: 9(최근 30일)
Say I have a code line in a live script that generates output.
I'd like to be able to copy the ouput
16 2 3 1
5 11 10 8
9 7 6 12
4 14 15 1
and past it a formatted text (monospaced, aligned, single line spacing) into the text of the script.
How do I do that?
  댓글 수: 4
Rik 2021년 3월 17일
Maybe formattedDisplayText (introduced in R2021a) is useful in this context. See this blog post for context.

댓글을 달려면 로그인하십시오.

채택된 답변

Alan 2021년 1월 29일
I think I worked out how to do it, but don't understand what's going on behind the sceens.
If I make a normal script file with the single command disp(magic(5)), then copy and past the output into the m-file as a comment after a section break.
% 17 24 1 8 15
% 23 5 7 14 16
% 4 6 13 20 22
% 10 12 19 21 3
% 11 18 25 2 9
Then save that file as a Live Code file I get what I want (see screen shot or the attached file.)
In the Live Code file, there seems to be a formated text box, which is monospaced. There doesn't seem to be a way of inserting such a box using the menu commands. However, a text box can be copied and pasted from one Live Code file to another, and then edited.
  댓글 수: 1
K. 2021년 3월 17일
The box you are seeing is likely a "Code Example". To insert one, go to the "INSERT" Toolstrip and find the "Code Example" button. (The MATLAB option gives you MATLAB syntax highlighting inside the box.)

댓글을 달려면 로그인하십시오.

추가 답변(2개)

Raynier Suresh
Raynier Suresh 2021년 1월 21일
편집: Raynier Suresh 2021년 1월 21일
You can use the evalc to evaluate MATLAB expression and capture results.
results = evalc('magic(5)')
You can also refer the below link for more information.
  댓글 수: 1
Rik 2021년 1월 21일
I'm not sure I would encourage people to use eval. Actually I am sure about my opinion. At the very least I would mention to be very careful.

댓글을 달려면 로그인하십시오.

K. 2021년 3월 17일
If I'm understanding your question correctly, the following might help. After running your live script, right click on the output and choose "Copy Output". Then paste it into a text (not code) area. If you don't have a line of text to paste it into, click the "Text" toolstrip button to insert a line of text first. For example:
If you'd then like to reduce the vertical spacing, you can do something like the other answer and convert it to a Code Example. Select the pasted text, go to the "INSERT" Toolstrip, and click the "Code Example" button:

Community Treasure Hunt

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

Start Hunting!

Translated by