TUTORIAL: How to format your question
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Andy
le 20 Mai 2011
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
- or numbered
- lists,
not to mention bold or italic text.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Historical Contests dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!