Problem with 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

5 个评论

For some weird reason, I can't upload any files. I get a msg saying I need to delete some files, but no clear to delete what and from where !
zip them up and use the paper clip icon.
I did, but got a msg saying can't upload that type of files. Also, at the moment, the website is blocking me from uploading any type of files, even Excel !
You already have uploaded 10 files today. Go to your previous question and delete the attached file there. After that, you will be able to attach files here.
I haven't uploaded any files ! This is the first question I post for over 2 weeks !!

请先登录,再进行评论。

 采纳的回答

‘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.

4 个评论

Many thanks for that. That was helpful. you mentioned i need to save my data to cell array. I have 12 files (all months of the year). Each of those files have about 11 columns and 30 rows. Am I meant to do this for each individual files ?
My pleasure.
If they are all the same sizes, you can save them as matrices, or as cell arrays. If they have different sizes (for different months, using eomday), a cell array for each month is how I would work with them. That way, you do not have to ‘adjust’ the sizes, and could get summary statistics for each month easily. If you want to put them in double arrays, you would still need to ‘pad’ the shorter months with NaN values to get them all to have equal lengths.
Cell arrays are efficient ways of storing data, but are inconvenient for calculations. You would need to use specific functions (such as cellfun) to work with them as cell arrays, or simply extract them into double arrays to work with them more easily.
What you do depends on how you are going to process your data, and how often you need to run your code.
Thank you very much Star for your help. Much appreciated !
As always, my pleasure!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by