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

8 visualizaciones (últimos 30 días)
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;
end
for i = 1:10
disp(i);
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:
[1,2,3,4,5,
will unfold any closed/folded for loops or if statements in my script.
  4 comentarios

Iniciar sesión para comentar.

Respuestas (3)

JWTay
JWTay el 5 de Mayo de 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:
if
end
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.
  2 comentarios
Nicholas Roberts
Nicholas Roberts el 4 de Abr. de 2018
Very perceptive JT! This helps me avoid the bug (still present in 2018a, ridiculously).
David Nguyen
David Nguyen el 26 de En. de 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)
end

Iniciar sesión para comentar.


Eric Lin
Eric Lin el 18 de Jun. de 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".
  3 comentarios
Sean Costello
Sean Costello el 12 de Dic. de 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 el 2 de Ag. de 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.

Iniciar sesión para comentar.


Amy
Amy el 10 de Dic. de 2018
Editada: Amy el 10 de Dic. de 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 comentario
Christopher Schlosser
Christopher Schlosser el 18 de Feb. de 2019
Editada: Christopher Schlosser el 18 de Feb. de 2019
I have version '9.5.0.944444 (R2018b)' and still get unwanted unfolding when creating functions or if-statements.
Edit: I updated to version '9.5.0.1033004 (R2018b) Update 2'. The problem still exists.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by