필터 지우기
필터 지우기

TUTORIAL: How to format your question

조회 수: 6 (최근 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
Andrew Newell
Andrew Newell 2011년 5월 20일
Naturally, I think that only those people who have voted for this page should be allowed to ask a question!
Seriously, I don't think we can force people to read it, but we can add a comment linking to it. A lot of people (including me) include links to Oleg's tutorial already.
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개)

카테고리

Help CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by