MATLAB: Problem with loading data

loading data

Hi all, I have a code that works for 28 days of each month of the year (because February has only 28 days mostly). I need to make it work for 31 days regardless of the month. When I change the number of days in the code from 28 to 31, (ndays = 31)
I am getting the following error: Index exceeds matrix dimensions. Error in Test_Data (line 85) Tmaxsurf(:,nmths) = Tmax(1:ndays); And the Tmaxsurf array shows most of the data as NAN Any idea how to load all the data for 31 days of each month? The code is long, so I attached attached the main coding files. I couldn't upload the data file as I am getting a msg saying that I am limited to 10 uploads per day. I haven't uploaded anything yet today at all !
Any help would be highly appreciated. Many thanks
% Initialise parameters
% First month to be investigated
mth1st = 7;
% First year to be investigated
yr1st = 2017;
%%Working with all data
% Loading data from file
% [dates,Tmin,Tmax,T9,T3] = BOMload('Dalby 2017-7.csv');
[dates,Tmin,Tmax,T3] = BOMload(['Dalby ',num2str(yr1st),'-',num2str(mth1st),'.csv']);
disp(' ')
% Total number of months to be tested
nmthtarget = 12;
% Store data from July as first lot of data. These variables need to be
% initialised since they are appended to
dateall = dates;
Tminall = Tmin;
Tmaxall = Tmax;
% Store minimum and maximum from July as current absolute min and max
% If all data is not to be stored, this is the way to find the min/max
Tminn = min(Tmin);
Tmaxx = max(Tmax);
% How many months have been tested so far
nmths = 1;
% Current year
yr = yr1st;
% Current month
mth = mth1st;
% Store data for the surface plot
% Initialise variables
ndays = 31;
datesurf = 1:ndays;
mthsurf = nan(1,nmthtarget);
yrsurf = mthsurf;
mthyrsurf = mthsurf;
Tmaxsurf = nan(ndays,nmthtarget);
% Store this month's data
mthsurf(nmths) = mth;
yrsurf(nmths) = yr;
mthyrsurf(nmths) = datenum(['01/',num2str(mth),'/',num2str(yr)], ...
'dd/mm/yyyy');
Tmaxsurf(:,nmths) = Tmax(1:ndays);
% Cycle through data
while nmths < nmthtarget
% Advance to next month
nmths = nmths + 1;
mth = mth + 1;
% If reached January, cycle to next year
if mth > 12
mth = 1;
yr = yr + 1;
end
% Load necessary data from file
[dates,Tmin,Tmax] ...
= BOMload(['Dalby ',num2str(yr),'-',num2str(mth),'.csv']);
% Append this month's data to the rest
dateall = [dateall;dates];
Tminall = [Tminall;Tmin];
Tmaxall = [Tmaxall;Tmax];
% % Record the min/max for the cumulative period (method if not storing
% % all the data)
% % Check if the lowest temperature for this month is the lowest so far
% Tminn = min(Tminn, min(Tmin));
% % Check if the highest temperature for this month is the highest so far
% Tmaxx = max(Tmaxx, max(Tmax));
% Surface plot: store this month's data
mthsurf(nmths) = mth;
yrsurf(nmths) = yr;
mthyrsurf(nmths) = datenum(['01/',num2str(mth),'/',num2str(yr)], ...
'dd/mm/yyyy');
Tmaxsurf(:,nmths) = Tmax(1:ndays);
end

Best Answer

‘Any idea how to load all the data for 31 days of each month?’
As you mentioned, not all months have 31 days. I would use the eomday (link) function for each month and year to determine the number of days in each month:
EOM_2018 = eomday(2018, 1:12)
EOM_2020 = eomday(2020, 1:12)
EOM_2018 =
31 28 31 30 31 30 31 31 30 31 30 31
EOM_2020 =
31 29 31 30 31 30 31 31 30 31 30 31
Saving your data to a cell array will allow for different length vectors. You can then fill the last days of months without 31 days with NaN or some other indicator.