title: Matlab integral function & function handles in a loop
body: <p>'Outliers.m' is called from a higher level .m file. The variables are all defined in the higher level file, and set as globals for access by Outliers.m. The purpose of the code is to identify outliers using Chauvenets Criterion, and for this, I have to calculate the integral of the guassian distribution, using the Integral function and function handles. The code works and gives sensible values when I enter specific variables as a test, but I cannot get it to work in a loop. My data set is comprised of 7 individual samples, each 1x30, all of which need to be analyzed. I have had various errors, read through the guidance on Integral and function handles, but cannot seem to find the solution...Any help or guidance would be very much appreciated.... Here is my code:</p>
  
  <pre><code>n = 7
  for x = 1:n
      for y = 1:30
      z(x,y) = abs((cc(x,y) - mastercc(1,y))/masterccstd(1,y));
      xmax(x,y) = mastercc(1,y)+z(x,y)*masterccstd(1,y);
      xmin(x,y) = mastercc(1,y)-z(x,y)*masterccstd(1,y);
      p(x,y) = 1/(masterccstd(1,y)*(sqrt(2*pi)));
  
      fun(x,y)= @(x,y,z) (exp(-1/2)*z(x,y).^2);
      q(x,y) = integral(fun(x,y),xmin(x,y),xmax(x,y),'ArrayValued',true);
  
      pq(x,y) = p(x,y)*q(x,y); % probability
      value(x,y) = n*(1/pq(x,y));
      count(x,y) = logical(value(x,y) &lt;0.5);
      badbins(x)=sum(count(x,:));
      end
  end
  </code></pre>