title: How to zero pad a matlab array?
body: <p>What is the easiest way to (zero) pad a matlab array?<br>
  i.e. given <code>[1,2,3,4]</code> and length 6 return <code>[1,2,3,4,0,0]</code></p>
  
  <h2>Background</h2>
  
  <p>I have a data array which I would like to apply a windowing function to before running fft on the data.</p>
  
  <p>I use to pass data directly to fft which would zero pad to the next power of 2, but now I need it zero padding before the fft so I can multiply by the window function.</p>
  
  <pre><code>fs = 100;                          % Sample frequency (Hz)
  t = 0:1/fs:10-1/fs;                % 10 sec sample
  x = (1.3)*sin(2*pi*15*t) ...       % 15 Hz component
    + (1.7)*sin(2*pi*40*(t-2)) ...   % 40 Hz component
    + (2.5)*randn(size(t));          % Gaussian noise;
  
  m = length(x);          % Window length
  n = pow2(nextpow2(m));  % Transform length
  w = barthannwin( n );   % FFT Window
  
  y = fft(data, n);       % DFT
  
  windowed_data = x*w ; % Dimensions do not match as x not padded
  y = fft(windowed_data, n); % DFT
  </code></pre>
  
  <p>I am aware of <a href="http://www.mathworks.co.uk/help/images/ref/padarray.html" rel="noreferrer">padarray</a> as part of the Image Processing Toolbox, which I do not have.</p>