title: Odd 'while' loop behavior with a subfunction call (MATLAB)
body: <p>So I have this bizarre problem that I cannot figure out.
  I have a while loop that is passing values into a vector according to an index. These values are provided by a subfunction that is called. My problem is that after the first iteration, the vector ceases to accept values from the subfunction, though the code keeps running and doesn't break down.
  This is happening at 'S(i)=Trials;'.</p>
  
  <pre><code>function Simulation()
  N=input('How many trials would you like to run? ');
  Tup=input('What is the positive boundary? ');
  Tdown=input('What is the negative boundary? ');
  StepLength=input('What is the step length? ');
  d=0;
  i=1;
  S=zeros(1,N);
  StepLimit=1000000;
  if Tup==Tdown
       display('Please use boundaries that are not equal to eachother.');
   elseif Tup&lt;=d||Tdown&gt;=d
           display('Please choose logical boundaries.');
  else
      while i&lt;=N
      S(i)=Trials;
      i=i+1;
      end
  end
  x=0:10:max(S);
  hist(S,x);
  axis tight;
  xlabel('Number of Steps');
  ylabel('Number of Trials');
  
  function s=Trials()
  s=0;
      while ~(d&lt;=Tdown)&amp;&amp;~(d&gt;=Tup)
          m=StepLength.*RandDir(1);
          d=d+m;
          s=s+1;
          if s&gt;=StepLimit
              display('The step limit was reached.');
              return
          end
      end
  
      function out = RandDir(N)
      % Generate a random vector from the set {+/- e_1, +/- e_2,..., +/- e_N}
      % where e_i is the ith basis vector. N should be an integer.
      I = round(ceil(2*N*rand));
          if rem(I,2) == 1
              sgn = -1;
          else
              sgn = 1;
          end
      out = zeros(N,1);
      out(ceil(I/2)) = sgn*1;
      end
  end
  end
  </code></pre>
  
  <p>Thank you for any help</p>