Ken & 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).


Ken & Mike work on the MATLAB Desktop team.
  • Ori: The current folder shortcut used to be alt-y. Now it is alt-o. However, while in the editor window, alt-o opens...
  • Jose Miguel: Hi First of all, thanks for the post, it is really useful. I am trying to develop a Java GUI within...
  • Ken: Hi Siddharth, There isn’t currently any way to move the docking controls. Feel free to submit an...
  • Phil: I have the same problem as described above with UITable working fine in the Matlab environment but showing no...
  • Siddharth: Is there any way to move the position of the docking controls (or eliminate them through some...
  • Chris: Yes, it is a challenge to organize functions into categories, especially with so many functions. Ken and I...
  • Mike: Thanks for fielding that one, Yair.
  • Yair Altman: Jimmy - if you mean that you wish to include hyperlinks in your function’s help comment, that will...
  • OysterEngineer: Thanks for explaining the Function Browser. I fired it up and gave it a try. It appears that it has...
  • Jimmy: Is there any way to include hyperlinks in a comment, such as the standard help at the beginning of a function?

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