2

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

1 Answer 1

2

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
...
8
  • Nice, that's good to know the full degree of control over folding that I can manipulate. The problem is that these code sections appear to only apply to blocks of "body" code rather than a set of functions spread across an m file. This is exactly what I'm being bombarded with in writing this complex GUIDE Fig file with a bunch of UIControls all over the place. I'd like to organize all the call backs, which I'm doing with the section headers but I can't figure out how to make those sections foldable. I suppose I could indent for readability, but folding is preferable here.
    – jxramos
    Jun 10, 2015 at 22:50
  • 1
    I looked at your updated post and your screenshot. Do you mean that your are unable to span a section across multiple functions? I'm not expert here, but could it be that there are multiple section delimiters (other than %%) and that somehow those are present above functions, in their documentation, forcing a new section? Just guessing here Jun 10, 2015 at 22:55
  • From the usage scenarios and the instructional video it appears the intent from Mathworks is to support call it "incremental batch line execution", but with functions there's no such equivalence in behavior.
    – jxramos
    Jun 10, 2015 at 22:57
  • Good lead to go on, see if there are any hierarchical section delimiters. I'll dig into the documentation to see what comes up.
    – jxramos
    Jun 10, 2015 at 22:58
  • 1
    Yeah I have mixed feelings about sections. They're used for multiple purposes: running a subset of code, documentation sections in publishing tool, and now folding code (with the above quirks). I would have preferred separate delimiters but a bit of digging into this makes me think there is only %% Jun 10, 2015 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.