How to format my code in this way? Is there any shortcut?

5 views (last 30 days)
dorian do
dorian do on 29 Nov 2021
Answered: Walter Roberson on 29 Nov 2021
The code is been put in a block and is not executable, but the syntax is been highlighted. So I was wondering how could I do this, is there a shortcut that I can convert a block of code into this format.
  1 Comment
Walter Roberson
Walter Roberson on 29 Nov 2021
Is the question about this Answers facility, or is it about the MATLAB editor?
Is the question about getting nice indentation?

Sign in to comment.

Answers (3)

John D'Errico
John D'Errico on 29 Nov 2021
If you put it in as a picture, then of course not!
However, if you paste in the code, then note there is a toolbar on top of the edit window when you are writing a comment or a question. Under that toolbar, you will see the word code. Does that give you a hint?
Next, click on this button in the code part of the toolbar, while you are editting.
You will see this appear in your edit window.
Now you can paste in any code you wish, into that spot. For example...
syms x
solve(x - 5 == 12)
ans = 
17
As you see, you can even tell it to evaluate that code in MATLAB.
  2 Comments
dorian do
dorian do on 29 Nov 2021
sorry for being unclear, I updated my question with some explanation.

Sign in to comment.


Image Analyst
Image Analyst on 29 Nov 2021
Not sure what you mean, but to indent your code properly, and transfer it from MATLAB to your Answers post, do this
  1. In your MATLAB editor window type control-a, which will select the entire editor window (all lines of code).
  2. Type Control-i, which will properly indent all the code.
  3. If you want to paste it into an Answers post, then type control-c to copy the selected code in the clipboard
  4. In the Answers text box, type control-v to paste in the clipboard (paste in your lines of code copied from the MATLAB editor window)
  5. Make sure all the code is highlighted in Answers and click the Code icon to format it as code. This will allow it to have a little Copy button so people can seasily copy your code into the clipboard and paste it back into MATLAB so they can test run your code.
  2 Comments
Image Analyst
Image Analyst on 29 Nov 2021
I agree with Walter's comment below your original question. We still don't know what you mean by a block, and where this code is sitting (MATLAB or the Answers forum).

Sign in to comment.


Walter Roberson
Walter Roberson on 29 Nov 2021
If you are asking whether there is a keyboard shortcut for starting one of these "Code Example" blocks, then unfortunately the answer is NO.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by