title: Functional decomposition advice need in Matlab
body: <p>I am learning matlab and am practicing functional decomposition. </p>
  
  <p>I basically had a long piece of code which worked, but I am trying to break it into smaller pieces with supporting function.</p>
  
  <p>I've spent ages debugging it, and have got my first supporting function 'readAndPrintMonths' working but am having problems with 'calculationLoop'</p>
  
  <p>error messages I am currently getting are:</p>
  
  <p><em>Error using month (line 36)
  Please enter D.</em></p>
  
  <p><em>Error in printMonthlyStats>calculationLoop (line 20)
      monthData = data(month, 3 : 33);</em></p>
  
  <p><em>Error in printMonthlyStats (line 6)
      calculationLoop(data, months)</em></p>
  
  <p><strong>Anyhere here is the code.</strong></p>
  
  <pre><code>function printMonthlyStats (inputCsvFilename)
  % Reads a csv file of daily rainfalls over a year.
  % Prints the total, max, and average rainfalls for each month.
  [data, months] = readAndPrintMonths(inputCsvFilename);
  for month = 1 : 12
  [total, mean, maxFall] = calculationLoop(data, months)
      end
  end
  
  function [data, months] = readAndPrintMonths(Filename)
      % creates all required headings
      data = csvread(Filename);
      months = ['Jan';'Feb';'Mar';'Apr';'May';'Jun';'Jul';'Aug';'Sep';'Oct';'Nov';'Dec'];
      fprintf('Monthly rainfall statistics\n');
      fprintf('Month Total  Max  Mean\n');
  end
  
  function [total, mean, maxFall] = calculationLoop(data, months)
      % runs a loop containing calculations for total mean and max rainfall
      monthData = data(month, 3 : 33);
      daysInMonth = data(month, 2);
      total = sum(monthData);
      mean = total / daysInMonth;
      maxFall = max(monthData);
      fprintf(' %3s %5.1f %5.1f %5.1f \n', months(month, 1:3), total, maxFall, mean);
  end
  </code></pre>