title: MATLAB: vectors of different length
body: <p>I want to create a MATLAB function to import data from files in another directory and fit them to a given model, but because the data need to be filtered (there's "thrash" data in different places in the files, eg. measurements of nothing before the analyzed motion starts). </p>
  
  <p>So the vectors that contain the data used to fit end up having different lengths and so I can't return them in a matrix (eg. x in my function below). How can I solve this?</p>
  
  <p>I have a lot of datafiles so I don't want to use a "manual" method. My function is below. All and suggestions are welcome.</p>
  
  <p><strong>datafit.m</strong></p>
  
  <pre><code>function [p, x, y_c, y_func] = datafit(pattern, xcol, ycol, xfilter, calib, p_calib,    func, p_0, nhl)
  
      datafiles = dir(pattern);
      path = fileparts(pattern);
      p = NaN(length(datafiles));
      y_func = [];
      for i = 1:length(datafiles)
          exist(strcat(path, '/', datafiles(i).name));
          filename = datafiles(i).name;
          data = importdata(strcat(path, '/', datafiles(i).name), '\t', nhl);
          filedata = data.data/1e3;
          xdata = filedata(:,xcol);
          ydata = filedata(:,ycol);
          filter = filedata(:,xcol) &gt; xfilter(i);
          x(i,:) = xdata(filter);
          y(i,:) = ydata(filter);
          y_c(i,:) = calib(y(i,:), p_calib);
          error = @(par) sum(power(y_c(i,:) - func(x(i,:), par),2));
          p(i,:) = fminsearch(error, p_0);
          y_func = [y_func; func(x(i,:), p(i,:))];
      end
  end
  </code></pre>
  
  <p><strong>sample data:</strong> <a href="http://hastebin.com/mokocixeda.md" rel="nofollow">http://hastebin.com/mokocixeda.md</a></p>