MATLAB Answers

Andrew Newell
13

TUTORIAL: How to format your question

Asked by Andrew Newell
on 20 May 2011
Latest activity Edited by Andrew Newell
on 25 Mar 2015
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 Comments

Nice and very useful, hopefully. How do we get newcommers to read this thread? What about increasing the reputation point if somebody *reads* this post and reject questions on a zero reputation? :-) Or a kind of gaptcha: Before a question is accepted, the above text must be typed in - of course with disabled copy&paste.
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.
I bump this thread hoping, that more newcomers will read it.

Sign in to comment.

1 Answer

Answer by Andy
on 20 May 2011
 Accepted Answer

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 Comment

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.

Sign in to comment.