I had exactly this problem without output from our matrix package.
My solution was to pre-process the output with a find-replace macro.
The macro finds the taglines, and replaces them with "end\n($1)"
That way, each block is wrapped in <tagline>...<end>
Then the folding is trivial.
I'm trying to set up folding on a DNA sequence
tagline1 [acgt]* ...multiple lines of above tagline2 [acgt]*