Search code examples
matlabcode-regions

Is there anything in Matlab analogous to C#'s #region / #endregion code collapsing structure?


I'm working with some matlab code and using good block comments to head off certain sections of the document, but it sure would be nice to just collapse the whole lot of sections once I'm done fleshing them out. General purpose code formatting region blocks may be a future feature request for Matlab too I suppose.

I recently discovered section breaks, but it looks too granular for my purposes one of which is to group a set of related functions. As far as I can see sections cannot embrace function definitions. By comparison C#'s #region code block formatting is general purpose and supports arbitrary nesting of any source content whatsoever.

Image showing how section breaks terminate at function keyword


Solution

  • There are a number of MATLAB constructs that can be folded with +/- signs in the editor. The closest thing to what you are looking for is maybe code-folding using sections, delimited by %%.

    It's not enabled by default in MATLAB, you need to go to:

    HOME > Preferences > Editor/Debugger > Code Folding
    

    There you'll see the list of enabled constructs - check the sections.

    Now delimit you code with sections and fold away!

    %% 
    Some code
    Some code
    %%
    Code in another section
    ...