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

4 次查看(过去 30 天)
dorian do
dorian do2021-11-29
回答: Walter Roberson ,2021-11-29
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 个评论
Walter Roberson
Walter Roberson 2021-11-29
Is the question about this Answers facility, or is it about the MATLAB editor?
Is the question about getting nice indentation?


回答(3 个)

John D'Errico
John D'Errico 2021-11-29
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 = 
As you see, you can even tell it to evaluate that code in MATLAB.
  2 个评论
dorian do
dorian do 2021-11-29
sorry for being unclear, I updated my question with some explanation.


Image Analyst
Image Analyst 2021-11-29
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 个评论
Image Analyst
Image Analyst 2021-11-29
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).


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


Community Treasure Hunt

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

Start Hunting!

Translated by