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.
  • DP: Hi i have a problem with ezplot3, i want to plot more than i curve in the same graph but hold on command...
  • Ken: Hi Arsalan, Unfortunately there is no way to get the new Editor API in older versions of MATLAB. -Ken
  • Arsalan: Hi, I am very excited about the MATLAB API for editor because right now i am working on a project and i need...
  • Johannes: Since I started using matlab-emacs some days ago I never experienced Emacslink. But I experienced some...
  • Francisco J. Beron-Vera: Hi all, I have recently learned about ViEmu (http://www.vimemu.c om) which, for Vi/Vim...
  • OysterEngineer: When I first learned of the Publish feature in MatLab, I thought it might be useful to help to...
  • Ken: Hi Herve, I’m not quite sure what you mean by “stand-alone&# 8221; mode? -Ken
  • Herve: I wonder when the publish fonction will be supported in standalone mode.
  • Mike: Ravi, What you described should work as far I understand it. Please follow up with technical support. With a...
  • Mike: @Daniel, Thanks for that note.

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