0
我正在學習matlab,正在實踐功能分解。Matlab中需要的功能分解建議
我基本上有一段很長的代碼,但我試圖用支持函數將它分解成小塊。
我花了年齡調試它,並得到了我的第一個支撐作用「readAndPrintMonths」的工作,但我有與我目前得到「calculationLoop」
錯誤消息的問題是:使用
錯誤月(第36行) 請printMonthlyStats輸入D.
錯誤> calculationLoop(第20行) monthData =數據(月,3:33);
錯誤printMonthlyStats(第6行) calculationLoop(數據,數月)
Anyhere這裏是代碼。
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