0% found this document useful (0 votes)
15 views2 pages

Part 2

The document reads in weather data from a CSV file, allows a user to enter a year, and analyzes the maximum temperatures and rainfall for each month of that year. It calculates the hottest, coldest, and wettest months and plots the average maximum temperatures and total rainfall for each month.

Uploaded by

Jaya Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Part 2

The document reads in weather data from a CSV file, allows a user to enter a year, and analyzes the maximum temperatures and rainfall for each month of that year. It calculates the hottest, coldest, and wettest months and plots the average maximum temperatures and total rainfall for each month.

Uploaded by

Jaya Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

% Import CSV file skipping first two rows

data = readtable('weather.csv', 'HeaderLines', 2);

% Extract relevant columns


years = data.Year;
months = month2num(data.Month);
rainfall = data.Rainfall;
max_temp = data.MaxTemperature;

% Prompt user to enter a year


user_year = input('Enter a year (between 1944 - 2022): ');

% Find indices corresponding to the user's entered year


indices = find(years == user_year);

if isempty(indices)
disp('Data for the entered year is not available.');
else
% Filter data for the entered year
year_months = months(indices);
year_rainfall = rainfall(indices);
year_max_temp = max_temp(indices);

% Calculate average maximum temperatures and total rainfall for each month of
the entered year
monthly_avg_max_temp = accumarray(year_months, year_max_temp, [], @mean);
monthly_total_rainfall = accumarray(year_months, year_rainfall, [], @sum);

% Plot average maximum temperatures for each month of the entered year
figure;
subplot(2, 1, 1);
bar(monthly_avg_max_temp);
title('Average Maximum Temperatures for Each Month');
xlabel('Month');
ylabel('Temperature (C)');
xticks(1:12);

% Plot total rainfall for each month of the entered year


subplot(2, 1, 2);
bar(monthly_total_rainfall);
title('Total Rainfall for Each Month');
xlabel('Month');
ylabel('Rainfall (mm)');
xticks(1:12);

% Find hottest month


[~, hottest_month] = max(monthly_avg_max_temp);
hottest_month_name = month_name(hottest_month);

% Find coldest month


[~, coldest_month] = min(monthly_avg_max_temp);
coldest_month_name = month_name(coldest_month);

% Find wettest month


[~, wettest_month] = max(monthly_total_rainfall);
wettest_month_name = month_name(wettest_month);

% Calculate total rainfall for the entire year


total_year_rainfall = sum(year_rainfall);
% Display results
fprintf('Hottest month: %s\n', hottest_month_name);
fprintf('Coldest month: %s\n', coldest_month_name);
fprintf('Wettest month: %s\n', wettest_month_name);
fprintf('Total rainfall for the entire year: %.2f mm\n', total_year_rainfall);
end

% Function to convert month names to numeric values


function monthNum = month2num(monthName)
months = {'January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December'};
monthNum = zeros(size(monthName));
for i = 1:numel(months)
monthNum(strcmp(monthName, months{i})) = i;
end
end

% Function to convert month number to name


function name = month_name(monthNum)
months = {'January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December'};
name = months{monthNum};
end

You might also like