Is there a way to 'fold up' sections of code - like when you close up a for loop

1,166 views (last 30 days)
Tom
Tom on 4 Apr 2012
Edited: Kelvin Prosyk on 9 Jul 2021
Is there a way to hide sections of code - like when you close up a for loop?
I have quite a long code with a few different sections, and while I'm working on one section I'd like to hide the other sections so the code is a bit more manageable to get around.
  6 Comments
Kelvin Prosyk
Kelvin Prosyk on 9 Jul 2021
Inserting artificial code loops inserts extraneous code and reduces readability if the code is published. The drawback of the existing %% code section approach is there is no end-section marker. It means you can't create an isolated code section in the same way as code blocks like a for loop. I'd love to seem Mathworks introduce a system of %%<section_start> and %%<section_end> tags.

Sign in to comment.

Accepted Answer

Daniel Shub
Daniel Shub on 4 Apr 2012
Under preferences you can enable code folding for blocks. Blocks are defined in MATLAB as sections starting with %% (I am not sure if they can have anything else on the line).
So you should be able to fold
a=1;
b=2;
x=3;
y=4;
  6 Comments

Sign in to comment.

More Answers (2)

Adam Spry
Adam Spry on 18 Mar 2017
Try this:
for d = 1
[Your code here]
end
its collapsable on demand and doesn't effect the code at all (someone please correct me if I'm wrong). It can be used as many times as you like for each section of the code.
  2 Comments
Walter Roberson
Walter Roberson on 11 Apr 2017
I think you mean "inside of control statements" rather than "instead of control statements" ?

Sign in to comment.


Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!