Doug's MATLAB Video Tutorials

December 9th, 2011

MATLAB tip: Use one command per line

This is a common programming pattern that I see with new MATLAB users: They put more than one command per line in MATLAB file. This can make it harder to visually take in a file, and because profiler and error messages give information back based on the line number this practice will often hide the source command if they are nested or placed two on a line.

4 Responses to “MATLAB tip: Use one command per line”

  1. Jonathan replied on :

    Doug,

    Does using one command per line incur an efficiency reduction?

    ~Jonathan

  2. doug replied on :

    @Jonathan,

    White space is purely visual. There is no downside to one command per line.

    Doug

  3. tunc replied on :

    Doug,

    the reasoning for Matlab not being able to output the erroneous command is not quite reasonable to me since Matlab parses those lines in order to find the seperation in between the command (which is the semicolon).

    So Matlab really *does* know which command is faulty!? So why not then output the suspicious command? Or can give me a more exhaustive reason why not?

    Tunc

  4. Doug replied on :

    Tunc,

    I am not sure what the reason is for only giving the line number. It might be that the nomenclature “line 34, 3rd command” is just not very compelling.

    What I do know is that code is easier to understand, and work with existing MATLAB reporting when it is one command per line.

    Doug

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

Doug's picture

These postings are the author's and don't necessarily represent the opinions of The MathWorks.