Ken & Mike on the MATLAB Desktop

June 15th, 2009

Fold whatever you want using cells

Juliette recently suggested that users should be able to fold whatever code they like. While this is a great feature idea, the MATLAB Editor doesn’t currently support folding arbitrary sections of code. You can, however, simulate the feature using cells.

Start by turing on Cell folding in Preferences -> Editor/Debugger -> Code Folding (folding cells is off by default). Now, take your raw code, which we’ll say looks like this:


and insert cell markers before and after the code you want to fold, like this:


Finally, collapse the cell and voila! You’ve folded an arbitrary chunk of code!


I realize this isn’t the ideal solution for folding a random chunk of code, but it’s a reasonable workaround in the meantime.

8 Responses to “Fold whatever you want using cells”

  1. steven replied on :

    I’ve always used folding in vim with the markers
    %%FOLDUP
    and
    %%UNFOLD

    I think code sharing would be much easier if there were a single standard, such as this one, which could be used in one’s editor of choice (including the matlab editor) and which persisted with the ascii file…

  2. Ken replied on :

    Hi Steven,

    Do you typically want your folds to be shared with other users? It seems that I’d want to remove folds before submitting or sharing code.

    -Ken

  3. Suresh replied on :

    Hi,

    I am sorry if this is off topic.

    But I find something very annoying in Matlab. The way matlab command history window displays, always “expanded”. Is there any way to set that it is always “collapsed”. It’s very tedious to scroll through it to a specific command in specific date. If it were always “collapsed” then I could go to specific date and expand it to see the list of command on that date.

  4. Ken replied on :

    Hi Suresh,

    Thanks for the feedback. Note that you can start typing a command in the Command Window and up arrow, which will then cycle through only the commands starting with what you’ve typed. I use this technique all the time to find previously typed commands.

    -Ken

  5. Alexander replied on :

    Hi,
    I’d love to do code folding! BUT, even after using cells in the code and enabling code folding in the preferences menu, there are no +/- signs to click on anywhere in my code to enable code folding.
    I’m writing a script .m file(not a function) and running:
    MATLAB Version 7.5.0.342 (R2007b)

    Also, I have cell highlighting on and it highlights correctly so I know that my cells are correct and the editor recognizes when my cursor is in one.

    Is this a version issue?

    thanks!

  6. Alexander replied on :

    Hi, Alexander here again,

    After a little research I found that when I try to code-fold manually by right-clicking and going to the code-folding option, all 4 options are grayed out and cannot be clicked on. Also, enabling/disabling code folding I noticed that when it is enabled my code is shifted over ~5pixels to, ostensibly, make room for the +/- graphics to be clicked on. However, as I mentioned before, there are no such signs.

    Also,I should probably mention that I am running this on a Windows XP 64bit architecture.

    doubly thanks,
    -alex

  7. Mike replied on :

    Alexander,

    Only functions were foldable in R2007b. Code folding for cells was added in R2008a.

  8. Alexander replied on :

    Thanks Mike, I’m downloading R2009b as we speak!

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.