MATLAB: Empty index from loop

emptyforindexloop

Hi everyone,
I currently have an n x 6 numeric matrix called meta. where site = meta(:,1), datenum = meta(:,2), variables = meta(3:10). There are numerous datenums that are identical as a consequence of looking at multiple sites over the same time period. I would like to sum each variable individually in accordance to the datenum. Below is the code that I have so far. I'm attempting to make a loop (see code below), however it keeps returning an empty index when I run the date_index=find(dates==i) line. I know that the dates will definitely match i (the unique dates), so confused as to why it's empty.
Equally, perhaps my loop within the loop needs tweaking? I'm very new to Matlab, so any help would be much appreciated. Also happy to provide further explanation if needed. Thank you in advance!
% Plot 1: Computing summary of sites by datenum
meta=xlsread('Incidents_and_HeadcountsUpdated.xlsx',4,'A2:L2123');
dates=(meta(:,2))
unique_dates=unique(dates)
%Sum of each individual variable per unique datenum
for i = 1:length(unique_dates),
date_index = find(dates==i);
for j = meta(3:10), %Columns for each incident type (rip, wind, tcur and tcut, total inc) and headcount (beach,water,total)
data_sums(i,1)=i;
data_sums(i,j-1) = sum(meta(index,j));
end
end

Best Answer

Importing your data as a table (which should automatically import dates as datetime):
meta = readtable('Incidents_and_HeadcountsUpdated.xlsx', 'Sheet', 4, 'Range', 'A:L'); %I'm assuming row 1 is header. Range may not even be needed
meta = groupsummary(meta, 2, 'sum') %instead of 2 you could use the actual name of the 2nd variable.

Instead of groupsummary, you could also use varfun:
meta = varfun(@sum, meta, 'GroupingVariables', 2) %instead of 2 you could use the actual name of the 2nd variable.
Both of the above assume that the dates you want to group are actually identical. If they differ by a small fraction and you want to bin them you can use groupsummary for that as well, but better would be to convert to timetable and retime it.