# MATLAB Spoken Here

## 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.

### 11 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 :

9. Kelvin replied on :

Hi everyone,

Is there a hotkey (shortcut) for collapsing and expanding code cells, so I wouldn’t need to manually click the tiny +/- sign?

Also, is there a way to quickly comment/uncomment code cells?

K

10. Mike replied on :

@Kelvin,
Yes. On windows Ctrl+. and Shift+Ctrl+. will fold/unfold the code segement. Ctrl+R and Ctrl+T will comment/uncomment code on windows.
To find out what had a keyboard shortcut or create one for an action, check out File -> Preferences -> Keyboard -> Shortcuts.
This also an excellent type of question for MATLAB Answers

11. Brandon replied on :

To fold up code, I add a simple if statement with an always positive expression (the number one). Of course a nice comment tells me what I folded up.

 Name (required) E-mail (required, will not be published) Website (optional) Spam protection (required): What is 2 + 3 ?

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

News from the intersection of MATLAB, Community, and the web.

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