title: In MATLAB: How can XData and YData be updated with a changing number of lines?
body: <p>I am looking for a way to add vertical dividing lines separating consecutive days in a dynamically updating plot of animal migration data (location vs. time).  Part of the challenge is that the number of these dividers changes as the plot domain expands to display more temporal data:  As the number of days in the plot increases from 3 to 5, for example, the number of dividers increases by 2.</p>
  
  <p>A minimal code example, written in MATLAB, is shown below:</p>
  
  <pre><code>xcols = [1; 1];
  ycols = [0; 1];
  
  figure(4)
  clf
  h.divs = plot(xcols,ycols,':k');
  xlabel('time')
  ylabel('location')
  for ii=2:6
      xcols(:,end+1) = [ii; ii];
      ycols(:,end+1) = [0; 1];
      % set(h.divs, 'XData', xcols, 'YData', ycols);
      % set(h.divs, {'XData'}, num2cell(xcols',2), {'YData'}, num2cell(ycols',2));
      drawnow
      pause(1)
  end
  </code></pre>
  
  <p>The problem centers on the two lines that have been commented out.  If I comment in the first of these to try to update XData and YData with each new set of dividers (given as the 2 x DividerCount matrices xcols and ycols), then I receive an error that these inputs "must be a vector of numeric type".  If I instead comment in the second line as way of using cell arrays to get around this (per <a href="https://stackoverflow.com/questions/33899003/how-can-you-set-the-xdata-of-a-matlab-line-graph-plot-with-a-2d-array">this Stack Overflow post</a> and <a href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/293082" rel="nofollow noreferrer">this MATLAB Newsgroup post</a>), then the code returns an error that "cell array handle dimension must match handle vector length" as soon as the number of dividers changes.</p>
  
  <p>Hacky solutions are certainly possible.  For example, the dividers can be plotted as a single line of horizontal and vertical segments, where the horizontal segments are placed above and below the y-axis limits of the plot.  Or a fixed number of dividers can be used, with some of the dividers plotted outside the x-axis limits of the plot.  The question is whether there is a non-hacky approach – one that can plot a potentially changing number of lines of identical style in the same figure with each pass of the loop.</p>