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 CommentsOldest to Newest
I’ve always used folding in vim with the markers
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…
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.
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.
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.
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 22.214.171.1242 (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?
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.
Only functions were foldable in R2007b. Code folding for cells was added in R2008a.
Thanks Mike, I’m downloading R2009b as we speak!
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?
Thank you in advance!
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
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.