title: Matlab search for string in a text file and print the next line in a LOOP
body: <p>I want to find a string <code>'Month'</code> in a text file that has numbers and other information e.g</p>
  
  <blockquote>
    <p>*My name is Dee</p>
    
    <p>*Month</p>
    
    <p>2 3 4</p>
    
    <p>3 6 8</p>
    
    <p>4 87 9</p>
    
    <p>*Day</p>
    
    <p>1, 4, 67,</p>
    
    <p>2, 6, 98,</p>
    
    <p>3, 55, 89,</p>
    
    <p>4, 12, 34,</p>
    
    <p>*das</p>
    
    <p>*gfsd</p>
    
    <p>*gdfd</p>
    
    <p>*sdasd</p>
    
    <p>*end of file</p>
  </blockquote>
  
  <p>and create an array of those numbers. Next, I want to search for next string, <code>'Day'</code>, and create an array of the corresponding numbers.  </p>
  
  <p>At the moment I am running a loop that searches all the characters twice and finds the numbers. This is making the program very slow. Can anyone please suggest a faster 
  way to do it? </p>
  
  <pre><code>strToSearchFor = 'Month'; 
  lengthStrToSearchFor = length(strToSearchFor); 
  MonthArrayFlag =0;
  
  for i=1:lengthAllFileChars
      fseek(fid9,i-1, 'bof');                                     
      tline = fscanf(fid9,'%c',lengthStrToSearchFor);
      if (strcmp(strToSearchFor,tline)==1)
          'FOUND IT';
          tline;
          for a = 1:lengthAllFileChars                        
              MonthNumber = fscanf(fid9, '%f,')';     
              if MonthArrayFlag == 0;
                  MonthArray = zeros(1,4);                     
                  MonthArray(1,:) =  nodeCoordinates;
                  MonthArrayFlag = 1;
              else
                  MonthArray = vertcat(MonthArray,MonthNumber);
              end
          end         
      end
  end
  </code></pre>
  
  <p>This code is then repeated for <code>'DAY'</code>.</p>