Ken & Mike on the MATLAB Desktop

April 28th, 2008

Code Folding Update in R2008a

We heard your requests, and have added 12 more foldable constructs, including full support for the new MATLAB class system. We introduced code folding back in R2007b (check out the blog entry on it here), and quickly heard that folding only functions and function help was not enough. This is a great example of how your feedback really can affect things.

All these new foldable constructs can really help to hide detail that you may not always need to see (possibly the internals of a switch/case block). This is especially useful if your working on two non-contiguous areas in a file. Normally, this would require lots of scrolling, but with code folding, you can just collapse all the constructs between the two areas (you could also use split screen, but I prefer the folding technique).

Here’s a look at edit.m, the same file Christina showed back in November 2007, only now there are a lot more foldable regions:

Code folding preferences

And the new code folding preferences page:

Code folding preferences

Just as with the original two foldable constructs, you can choose which constructs you’d like to be folded by default. So if you want switch/case blocks to be folded whenever you open a file, select Fold Initially next to Switch/case blocks in the Editor/Debugger -> Code Folding preferences pane.

Also note that once you’ve opened a a file and tweaked what’s folded and what’s expanded, the MATLAB Editor will remember the fold state of that file even after you’ve closed it. So the next time you open your file, everything will be just as you left it.

If there are things you think should be foldable, that we’re not folding, leave us a comment here.

6 Responses to “Code Folding Update in R2008a”

  1. Han Geerligs replied on :

    Hello Ken,

    how about introducing the “region” concept as used in the Visual Studio Environment?
    This enables the user to introduce his own “folding blocks”.

    For more info please look at
    http://msdn.microsoft.com/en-us/library/9a1ybwek(VS.71).aspx

  2. Ken replied on :

    Hi Han,

    Thats an interesting idea, one also present in Xcode via the #pragma mark token. There’s a school of thought that says you shouldn’t dirty your code in an effort to accommodate your IDE (check out Jeff Atwood’s article on code folding), though I’m not quite sure where I stand on the issue yet. I will submit an enhancement request though!

    -Ken

  3. Kay replied on :

    why not make single “else”s foldable. You could work on one case, without being botherd with what happens in the other cases, but being able to see the conditions of the other cases/elses.

  4. Ken replied on :

    Hi Kay,

    We decided that an if block including all it’s elseif and else statements is a single logical entity, and should be folded as such. To get the behavior you’re after, I’d encourage you to break out the internals of your if/elseblocks into sub-functions or inner functions, each of which are foldable.

    -Ken

  5. Juliette Salexa replied on :

    We should be able to fold whatever we want. Not just those listed above. I should be able to highlight a few lines, and hide them .. sometimes I don’t want to see huge blocks of code that are irrelavent to the current situation, and these blocks of code are not appropriate to incorporate into functions.

    Whether or not it’s considered “dirty” or “sloppy” programming style, I like programming in a certain way, and I’m the only one using my programs, so I should be allowed to do what I want!

    In Maple I can hide whatever I want, why not in MATLAB ??

  6. Ken replied on :

    Hi Juliette,

    Note that you could use cells in order to fold chunks of code you don’t want to see. While this isn’t the selection based folding your asking for, it is a reasonable workaround. To fold a block of code, you could insert cell markers like this:

    %% cell start
    % code to fold
    %% cell end

    -Ken

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.