MATLAB Answers

Extract Min, Max and mean values for each month of each respective year. (Excel Data)

15 ビュー (過去 30 日間)
Ahmed M'sallem
Ahmed M'sallem 2019 年 11 月 22 日
Edited: Andrei Bobrov 2019 年 11 月 22 日
Using the daily temperature data I would like to be able to extract mean monthly temperature, extreme minimum monthly temperature (the coldest temperature of the month), and extreme maximum monthly temperature (the hottest temperature of the month).
For example, for the month of January, I would like to be able to values the min, max and mean for each year.
I would please like an example shown for january then I will proceed

  0 件のコメント

サインイン to comment.

件の回答 (3)

Walter Roberson
Walter Roberson 2019 年 11 月 22 日
One of the easier ways is to readtable() and convert to timetable() object, and use retime()

  0 件のコメント

サインイン to comment.


Erivelton Gualter
Erivelton Gualter 2019 年 11 月 22 日
%% Initialize variables.
filename = 'temptable.csv';
delimiter = ',';
startRow = 3;
%% Format for each line of text:
formatSpec = '%f%f%f%f%C%f%s%f%C%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to the format.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'EmptyValue', NaN, 'HeaderLines' ,startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
%% Close the text file.
fclose(fileID);
%% Create output variable
temptable = table(dataArray{1:end-1}, 'VariableNames', {'Year','Month','Day','MaxTempC','MaxTempFlag','MinTempC','MinTempFlag','MeanTempC','MeanTempFlag'});
%% Clear temporary variables
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
%% HERE YOU CAN ADD A FOR LOOP AND GET THE INFO FOR OTHER MONTHS
i=1; % January
idx_months = find(temptable.Month == i);
meanmonthly = mean(temptable(idx_months,:).MeanTempC(~isnan(temptable(idx_months,:).MeanTempC)));
minmonthly = min(temptable(idx_months,:).MeanTempC(~isnan(temptable(idx_months,:).MeanTempC)));
maxmonthly = max(temptable(idx_months,:).MeanTempC(~isnan(temptable(idx_months,:).MeanTempC)));

  2 件のコメント

Ahmed M'sallem
Ahmed M'sallem 2019 年 11 月 22 日
I feel like I have a better understanding on the approach, but as I checked the result, it turns that the mean, max and min values are for the entire set of data, but I would like the min max and mean values for each year based on each month. Maybe I have misunderstood your code.
Erivelton Gualter
Erivelton Gualter 2019 年 11 月 22 日
You are right. It was misshing the year.
Change the following line:
idx_months = find(temptable.Month == i & temptable.Year == 1977);
The results correspond to Janury 1997.

サインイン to comment.


Andrei Bobrov
Andrei Bobrov 2019 年 11 月 22 日
編集済み: Andrei Bobrov 2019 年 11 月 22 日
Please run file MATLABAnswer.m
MATLABAnswer.m:
T = readtable('Path\to\your\file\temptable.csv','Delimiter',',',...
'HeaderLines',1);
T = fillmissing(T(:,[1:3,4:2:end-1]),'linear'); % for NaNs
Tout = rowfun(@funanswer,T,'InputVariables',4:6,'GroupingVariables',1:2,...
'OutputVariableNames',{'max_monthly','min_monthly','mean_monthly'});
function [a,b,c] = funanswer(x,y,z)
a = max(x);
b = min(y);
c = mean(z);
end

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。


Translated by