How can I add a 2-column legend to a Matlab plot?
<p>Consider following code:</p>
<pre><code>t=0:.01:(2*pi);
y=[sin(t);sin(t-pi/12);sin(t-pi/6);sin(t-pi/4)];
figure(1)
    clf
    subplot(6,1,5)
    plot(t,y)
    xlim([0 2*pi])
    legend('1','2','3','4')
</code></pre>
<p>It produces following figure:</p>
<p><a href="https://i.stack.imgur.com/ng5yD.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ng5yD.png" alt="![enter image description here" /></a></p>
<p>Is there a way to change the legend to a 2-column lay-out? So it would be</p>
<blockquote>
<p>--- 1 --- 3</p>
<p>--- 2 --- 4</p>
</blockquote>
<p>instead of</p>
<blockquote>
<p>--- 1</p>
<p>--- 2</p>
<p>--- 3</p>
<p>--- 4</p>
</blockquote>
<p>so the legend boundary lined would not cross the graph boundary lines.</p>
<p>I found the <a href="http://www.mathworks.com/matlabcentral/fileexchange/29248-gridlegend-a-multi-column-format-for-legends" rel="noreferrer"><code>gridLegend</code></a> script, but I prefer to code it directly.</p>