Clear Filters
Clear Filters

Matlab unfolds folded code when I leave an open for/if loop (temporarily omit the "end" while working)

14 views (last 30 days)
I recently updated to Matlab 2015a, coming from a late 2013 or early 2014 build. I have noticed one disruptive change in particular. When I write code, I will often fold previous segments when finished, with some comment to explain what the fold contains. I will then continue writing code in later sections.
I have noticed that now when I continue working on later parts, if I make a new statement that requires an "end", such as a for loop or if statement, Matlab will automatically unfold previously folded for loops and if statements if I type much code without adding the "end." For example:
for fold = 1
z = 2;
for i = 1:10
If I fold the first for loop and keep working on code inside the second for loop without appending an "end," after a line or two of new code Matlab automatically unfolds the first for loop. Please let me know if this isn't clear. I have tried searching for some setting that governs this behavior, but have not found anything, and don't know what this sort of automatic behavior by Matlab would even be called!
Edit: I checked again, and it also does this automatic unfolding if I do something like leave a bracket open. Typing:
will unfold any closed/folded for loops or if statements in my script.

Sign in to comment.

Answers (3)

JWTay on 5 May 2017
I've been encountering this bug and I did some testing and found that it occurs when there are at least two unmatched statements/brackets.
For example: Writing 'if aa == someValue' will trigger the code to unfold when the first equals sign is typed in because now there are two unmatched statements (the 'if' statement and the following expression).
A workaround would be to close the if statement first, before writing the expression to be evaluated. E.g. writing:
before writing the evaluation expression (aa == someValue) will keep your code closed.
Keep in mind though that the two unmatched statements do not have to occur in the same place. So if you had an open if/function/class/bracket somewhere else, it will still cause the code to unfold.
Hope this makes sense and helps in the meantime. Fingers crossed that MathWorks can solve this issue as it is pretty frustrating when working on large files.
David Nguyen
David Nguyen on 26 Jan 2019
You could also (1) pre-comment (using the ctrl+R keyboard shortcut) some lines, onto which you can (2) type the scaffold structure of the if statement, then (3) uncomment (using the ctrl+T keyboard shortcut). After this, you can modify the if statement's condition like usual.
Works in App Designer 2018b.
(1) Pre-comment (using the ctrl+R keyboard shortcut)
(2) Type the scaffold structure of the if statement
% if (true)
% end
(3) Uncomment (using the ctrl+T keyboard shortcut)
if (true)

Sign in to comment.

Eric Lin
Eric Lin on 18 Jun 2015
This is a known bug in MATLAB. The only workaround right now is to disable code folding for the particular constructs that experience this issue by going to "Preferences" > "Editor/Debugger" > "Code Folding".
Sean Costello
Sean Costello on 12 Dec 2016
This workaround doesn't work for me (Matlab 2015b). Opening an if statement causes all code in the file to unfold.
Chuck Saunders
Chuck Saunders on 2 Aug 2018
I wish this were fixed. This was a known bug... three years ago now? It's still a problem.
> "The workaround to prevent your code from unfolding is to disable folding."
That's not a workaround at all. "The software doesn't have bugs if you don't use it." Trivial advice.

Sign in to comment.

Amy on 10 Dec 2018
Edited: Amy on 10 Dec 2018
This bug is fixed as of R2018b. If you are still running into issues with unwanted code unfolding in R2018b or a later release, please feel free to contact technical support.
  1 Comment
Christopher Schlosser
Christopher Schlosser on 18 Feb 2019
Edited: Christopher Schlosser on 18 Feb 2019
I have version ' (R2018b)' and still get unwanted unfolding when creating functions or if-statements.
Edit: I updated to version ' (R2018b) Update 2'. The problem still exists.

Sign in to comment.


Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!