Comparing each row of a table to the same row of a different table. Then store the highest maximum temperature, lowest minimum temperature from that row.

11 次查看(过去 30 天)
I have 3 years of data from 1981 through 1983. Each file is in text format labeled as temp_summary.05._%month_%year.txt. Of each file, has four columns (day, max_t, min_t, and avg_t) with the corresponding number of days for each month (31 days for January and etc.). I need to compare row 1 through x to the same corresponding row of the following years. For example, 1981 January row 1 (day 1) to 1982 January row 1 then to 1983 January row 1. The goal is to retrieve the highest temperature and lowest temperature of those rows then store into a different array. The following code works as it navigates through each file effortlessly while ignoring missing data in the tables. However, it's not retrieving the max temperature of that row but instead storing all maximum and minimum temperatures without the dates that it occurred on. What can I do? Need help.
clear all;
clc;
startyear = 1981;
endyear = 1983;
hightemps = [];
lowtemps = [];
avgtemps = [];
for year = startyear:endyear
for month = 1:12
for day = 1:31
filename = sprintf('temp_summary.05.%02d_%02d.txt', month, year);
data = readtable(filename);
data.Properties.VariableNames([1 2 3 4]) = {'Day' 'Maximum T' 'Minimum T' 'Average T'}
daydata = data(1:31,"Day");
hightemp = max(data(1:31,"Maximum T"));
lowtemp = min(data(1:31,"Minimum T"));
meantemp = mean(data(1:31,"Average T"));
DD = rmmissing(daydata);
HT = rmmissing(hightemp);
LT = rmmissing(lowtemp);
MT = rmmissing(meantemp);
hightemps = [hightemps;HT];
lowtemps = [lowtemps;LT];
avgtemps = [avgtemps;MT];
end
end
end
  9 个评论
Cris LaPierre
Cris LaPierre 2023-10-18
MT is a table with 2 dimensions, so you have to either index using (row,column) notation, or index a specific variable in the table.
MT(MT. Month == 1,"VarName")
% or
MT.VarName(MT.Month == 1)

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2023-10-18
I would use groupsummary.
Tbl1 = readtable("temp_summary.05.01_1981.txt","ConsecutiveDelimitersRule","join");
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
Tbl1.Day = datetime(1981,5,Tbl1.Day);
Tbl2 = readtable("temp_summary.05.01_1982.txt","ConsecutiveDelimitersRule","join");
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
Tbl2.Day = datetime(1982,5,Tbl2.Day);
Tbl3 = readtable("temp_summary.05.01_1983.txt","ConsecutiveDelimitersRule","join");
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
Tbl3.Day = datetime(1983,5,Tbl3.Day)
Tbl3 = 31×4 table
Day MaximumTemperature MinimumTemperature AverageTemperature ___________ __________________ __________________ __________________ 01-May-1983 67 10 38.5 02-May-1983 65 24 44 03-May-1983 65 16 40.5 04-May-1983 59 21 40 05-May-1983 53 22 37 06-May-1983 59 16 32.5 07-May-1983 63 21 41 08-May-1983 58 10 32 09-May-1983 62 13 35.5 10-May-1983 65 17 40 11-May-1983 59 24 41 12-May-1983 58 38 48 13-May-1983 63 25 45.5 14-May-1983 65 17 42 15-May-1983 56 17 39 16-May-1983 58 21 42.5
sumTbl = groupsummary([Tbl1;Tbl2;Tbl3],["Day","Day"],["monthofyear","dayofmonth"],["max","min"],["MaximumTemperature" "MinimumTemperature"])
sumTbl = 31×7 table
monthofyear_Day dayofmonth_Day GroupCount max_MaximumTemperature min_MaximumTemperature max_MinimumTemperature min_MinimumTemperature _______________ ______________ __________ ______________________ ______________________ ______________________ ______________________ 5 1 3 67 63 12 10 5 2 3 70 65 28 24 5 3 3 67 65 17 16 5 4 3 61 57 22 21 5 5 3 58 46 24 20 5 6 3 63 59 18 16 5 7 3 63 60 25 21 5 8 3 60 58 20 10 5 9 3 64 62 13 11 5 10 3 65 64 17 12 5 11 3 59 54 999 22 5 12 3 60 58 40 23 5 13 3 66 63 30 25 5 14 3 65 63 19 17 5 15 3 61 56 19 16 5 16 3 64 58 23 21
From there, you can extract the columns (variables) you want. See here: https://www.mathworks.com/help/matlab/matlab_prog/access-data-in-a-table.html
maxTempMay = sumTbl.max_MaximumTemperature
maxTempMay = 31×1
67 70 67 61 58 63 63 60 64 65
minTempMay = sumTbl.min_MinimumTemperature
minTempMay = 31×1
10 24 16 21 20 16 21 10 11 12
  93 个评论
Cris LaPierre
Cris LaPierre 2024-2-5
编辑:Cris LaPierre 2024-2-6
It appears all your February files have data for the 29th, but only 2016 is a leap year. This is leading to 2 rows of data for Mar 1 in 1981 and 1982.
Cris LaPierre
Cris LaPierre 2024-2-6
I do not have access to the data for that month & year so can't investigate why that is happening. However, you should learn how to do this. I propose the following steps
  1. Inspect the raw text file. Is there a temp of 157 in March 1999?
  2. Inspect the dataAll table. Is there a temp of 157 for March 1 1999?
  3. Inspect the minT_tbl table. Is there a temp of 157 in the row for March 1?
Once you've figured out the source of the 157, next determine why it is being selected, or perhaps figure out what the correct value should be. This might mean looking at the raw files, etc. Once you know what the correct value shoudl be, and where the incorrect values are appearing, you can look at the relevant code and figure out what you need to change to fix it.

请先登录,再进行评论。

更多回答(0 个)

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by