0

I am learning matlab and am practicing functional decomposition.

I basically had a long piece of code which worked, but I am trying to break it into smaller pieces with supporting function.

I've spent ages debugging it, and have got my first supporting function 'readAndPrintMonths' working but am having problems with 'calculationLoop'

error messages I am currently getting are:

Error using month (line 36) Please enter D.

Error in printMonthlyStats>calculationLoop (line 20) monthData = data(month, 3 : 33);

Error in printMonthlyStats (line 6) calculationLoop(data, months)

Anyhere here is the 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
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

2

You are not passing the variable month to calculationLoop which causes the function to call the build in function instead of your variable.

Daniel
  • 36,610
  • 3
  • 36
  • 69