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

755 ビュー (過去 30 日間)
Tom
Tom 2012 年 4 月 4 日
コメント済み: Caleb 2024 年 1 月 30 日
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.
  7 件のコメント
Kelvin Prosyk
Kelvin Prosyk 2021 年 7 月 9 日
編集済み: Kelvin Prosyk 2021 年 7 月 9 日
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.
Eric Daigrepont
Eric Daigrepont 2022 年 1 月 23 日
編集済み: Eric Daigrepont 2022 年 1 月 23 日
@Kevlin Prosyk, I agree wholeheartedly. I try to use "%%" to held organize collapsible sections, but the auto-end-section significantly limits the ability to manage sections due to erratic updates and inconsistent operation. Frankly, one of the earliest languages I ever used, Pascal maybe, used "//" to comment the remainder of the line, and "/* ... */" to comment sections. I've been annoyed by any language since that did not allow the programmer to comment sections. And with the added benefit of modern code-folding,... controlling exactly how you'd like to fold you sections would add wonderfully for those of us love of clean, organized code.

サインインしてコメントする。

採用された回答

Daniel Shub
Daniel Shub 2012 年 4 月 4 日
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;
  10 件のコメント
Manuel
Manuel 2023 年 11 月 8 日
Thank you so much!
Caleb
Caleb 2024 年 1 月 30 日
Absolute W thank you

サインインしてコメントする。

その他の回答 (2 件)

Adam Spry
Adam Spry 2017 年 3 月 18 日
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 件のコメント
Christopher Goodrich
Christopher Goodrich 2017 年 4 月 11 日
I recently discovered this trick has its shortcomings. You can't define functions instead of control statements (even if it is just a loop that doesn't actually loop).
I wish that you could define the end of code sections so you could nest code sections inside of code sections.
Walter Roberson
Walter Roberson 2017 年 4 月 11 日
I think you mean "inside of control statements" rather than "instead of control statements" ?

サインインしてコメントする。


Ethan Duckworth
Ethan Duckworth 2021 年 2 月 25 日

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by