Mike on the MATLAB Desktop

September 22nd, 2008

MATLAB pragmas

In a recent comment, Han Geerligs suggested we introduce #pragma-type directives to introduce arbitrary code folding. Well, we do have that, it’s called cells. You can introduce a cell by placing a %% at the beginning of a line. You may also need to turn code folding on for cells in order for this to work (see this blog entry for instructions). It’s not exactly the same as a #region, but it has some nice MATLAB-y features, such as evaluation and shading in the editor (see this blog entry for more info).

Other directives like the “#” compiler directive in MATLAB are indicated with the %# token. In R2008a, the MATLAB language supports the following directives:

  1. %#ok – suppress M-Lint messages
  2. %#eml – puts a M-file in to Embedded MATLAB mode
  3. %#function and %#external for the MATLAB compiler

Since these directives are also MATLAB comments, you can easily put additional comments on the same line explaining what the token is doing there.

Please don’t abuse cells for the sake of folding. If your code is too big, for your eyes’ sake, break up the file into separate functions.

2 Responses to “MATLAB pragmas”

  1. Han Geerligs replied on :

    Hello Mike,

    first, thanks for mentioning me ;-)

    I was wondering if you plan to introduce more pragma type directives in the upcoming release?

    kind regards, Han

  2. Mike replied on :

    Han,

    We are always investigating new ways to enhance the MATLAB language and improve the desktop experience. I can’t comment on any specific plans, we try to balance the advantage that the meta information supplies as well various editing models versus cluttering up one’s MATLAB file with non-code text and complicating the language.

    If the pragma usage expands, I would expect it come from toolboxes that provide different ways of running a particular file.

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
Mike works on the MATLAB Desktop team.

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