MATLAB Answers

to determine rainfall data

8 views (last 30 days)
dira azman
dira azman on 12 Dec 2019
Commented: Walter Roberson on 23 Dec 2019
hi, i have rainfall data in txt file. the data has 100 years of data. i want to determine the wettest and driest year, maximum, minimum and average for years and month.
to solve this i need to using loop function. with this i attach the data.
thanks.

  0 Comments

Sign in to comment.

Answers (1)

Akira Agata
Akira Agata on 12 Dec 2019
I would recommend using timetable and retime functions, rather than using for-loop.
The following is an example:
% Read data
Data = readmatrix('rainfall.txt');
% Create timetable variable
Time = datetime(Data(:,1),Data(:,2),1);
Rainfall = Data(:,3);
TT = timetable(Time,Rainfall);
% Create yearly data
TTsum = retime(TT,'yearly','sum');
TTavg = retime(TT,'yearly','mean');
TTmax = retime(TT,'yearly','max');
TTmin = retime(TT,'yearly','min');
figure
subplot(2,1,1)
plot(TT.Time,TT.Rainfall,'.')
hold on
stairs(TTmax.Time,TTmax.Rainfall)
stairs(TTavg.Time,TTavg.Rainfall)
stairs(TTmin.Time,TTmin.Rainfall)
grid on
legend({'Original data','Max. for each year','Avg. for each year','Min. for each year'},...
'NumColumns', 2,...
'FontSize', 10)
subplot(2,1,2)
stairs(TTsum.Time,TTsum.Rainfall)
legend({'Sum. for each year'},...
'FontSize', 10)
grid on
rain.png

  4 Comments

Show 1 older comment
Akira Agata
Akira Agata on 14 Dec 2019
Could you tell us why you need to use for-loop?
dira azman
dira azman on 23 Dec 2019
its my assignment task
the question said need to use fo-loop
Walter Roberson
Walter Roberson on 23 Dec 2019
if isempty(YourArray)
minval = [];
else
minval = inf;
for K = 1 : numel(YourArray);
if YourArray(K) < minval
minval = YourArray(K);
end
end
end

Sign in to comment.

Sign in to answer this question.


Translated by