TUTORIAL: How to format your question

18 ビュー (過去 30 日間)
Andrew Newell
Andrew Newell 2011 年 5 月 20 日
編集済み: Andrew Newell 2015 年 3 月 25 日
A lot of questions include code that looks like this:
A = []; for ii = 1:10 A = [A ii]; end
This makes it difficult for people who are trying to answer the question because they have to reformat the code before running it.
Many questions also have text that looks like this.
When you are composing a question, you can click on Markup help to find out how to format your code properly. However, many people may not understand how to use this formatting because there are no examples. In this tutorial I will provide some examples, but first I'll state the most important point:
Look at the preview window!
The Preview window is below the window you are typing your question into. Make sure your browser is scrolled down far enough to see it.
Now let's go back to the badly formatted code. Add two spaces at the start of the first line and it should look like this in the Preview window:
A = [];
for ii = 1:10
A = [A ii];
end
You don't need to indent after every blank line, but you do need to indent every time there is a blank line between lines of code. For example, if you type something that looks like this:
A = [];
for ii = 1:10 A = [A ii]; end
it is because you have a blank line between the first command and the for loop. You should indent the for two spaces to get this:
A = [];
for ii = 1:10
A = [A ii];
end
Code markup has (at least) one undocumented feature. Suppose you like to format loops so the code inside is indented. If you put two spaces in front of A = [A ii];, you'll see no difference. You need to indent the code two spaces plus the number of spaces you want to see. Indent four spaces and you'll get this:
A = [];
for ii = 1:10
A = [A ii];
end
Finally, if you see text that looks like this, get rid of the spaces at the beginning of the line.
To repeat, the bottom line is: Look at the Preview window!
EDIT: If you have multiple lines of code with some blank lines in the middle, it will be easier to just highlight the code with your mouse and press the {} Code button.
  3 件のコメント
Jan
Jan 2012 年 7 月 22 日
I bump this thread hoping, that more newcomers will read it.

サインインしてコメントする。

採用された回答

Andy
Andy 2011 年 5 月 20 日
Is this tutorial supposed to be just about how to format code, or is it how to use formatting in general? If the latter, then there should probably be something about using links to helpful documents, or
  • Creating
  • bulleted
  1. or numbered
  2. lists,
not to mention bold or italic text.
  1 件のコメント
Andrew Newell
Andrew Newell 2011 年 5 月 20 日
I decided to focus on formatting code, because that is the most important issue and it gives people the most trouble. However, answers like yours could explore the other things that can be done.

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by