title: creating a legend within for loop for Matlab plot
body: <p>I want to plot several curves, each having a different length. So, I've placed each curve as an array in a cell index, Y (this allows me to index through arrays of different sizes inside a FOR loop). I use "hold all" below to enable each iteration of the FOR loop to plot each new array in the cell array Y inside the same plot. </p>
  
  <pre><code>hold all;
  for i = 1:1:length(maxusers)
    time = increment*(0:1:length(Y{i})-1);
    plot(time,Y{i,1,:});
  end
  </code></pre>
  
  <p>While the use of a cell array here does simplify plotting the various curves inside Y, the problem I'm having is creating legend. Currently I'm using a really long/ugly switch statement to cover every possible scenario, but I <em>think</em> there should be a more elegant solution. </p>
  
  <p>If I have an array (where maxusers=4, for example) that is:</p>
  
  <pre><code> filesize = [10 100 200 300];
  </code></pre>
  
  <p>I know the legend Matlab command that works is:</p>
  
  <pre><code>legend(num2str(filesize(1)),num2str(filesize(2)),num2str(filesize(3)),num2str(filesize(4)));
  </code></pre>
  
  <p>but I get stuck trying to create a legend command when the number of curves is a variable given by maxusers. Any ideas? Thanks in advance.</p>