Extract array information from date and create cell

2 views (last 30 days)
Hi there,
I am having a little difficulty and was wondering if anyone had done something similar. I have a very large array (imported from Excel); 4884x2. This is data for each day over a 6 month period. I have also imported the dates, used datenumber and added them to the matrix. For each day there are elements every 15 minutes (new row). Unfortunately there are times missing resulting in reshape not being correct.
I am trying to create a cell array where each cell is an individual dates data (i.e all the time readings for the 4th of August).
My code currently reads in the excel file, dates and times.
I then
dates = datetime(datestr(dates));
days = day(dates); unidays = unique(days);
months = month(dates); unimonths = unique(months);
Followed by
datenumber = datenum(dates);
dt = unique(datenumber);
a = horzcat(datenumber,data);
for ii = 1:length(dt)
for iii = 1:32
% for iiii = length(data)
v{ii} = data(iii,:);
% end
end
end
An example of the excel sheet (only an extract) is:
2017/03/27 09:15 23.32362362 25.36263524
2017/03/27 09:30 14.26554124 52.36512337
2017/03/27 09:45 45.97456444 75.25463254
2017/03/27 10:00 41.69875632 37.95423965
With times going on to later in the day.
Thanks
  1 Comment
Luke McLellan
Luke McLellan on 13 Feb 2019
iii = 1:32 is wrong. If all data was there then this would be the number of times per day.

Sign in to comment.

Accepted Answer

Luke McLellan
Luke McLellan on 19 Feb 2019
Thanks Guillaume.
I have been able to separate the dates into a cell a different way. After reading the infor in I created an array of date (no time) number and the 2 corresponding columns. I then ran a unique on the dates and search for the index occurence. Once located I grabbed the data from the 2 other columns and created a cell in this manner.
Next steps for me are introducing more time analysis throughout the days.
Thanks again!
  3 Comments
Luke McLellan
Luke McLellan on 20 Feb 2019
Edited: Luke McLellan on 20 Feb 2019
Hi Guillaume, I was able to separate each days data and described how I done it encase others ever have this issue. I never accepted your anwser because I never used it, otherwise I would have.
Guillaume
Guillaume on 20 Feb 2019
because I never used it, otherwise I would have.
That's the thing I don't understand. You come here asking for help, then completely ignore that help and use a complicated way of doing your thing. Again, it's rarely a good idea to split your data into multiple arrays/cell arrays/data. Matlab has plenty of function that can perform aggregration according to some grouping. You've never fully explained what you were doing, but for example loading an excel file and calculating the monthly average is just 2 lines:
data = table2timetable(readtable('somefile.xlsx')); %read the file into a timetable
monthlyavg = retime(data, 'monthly', 'mean'); %average by month

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 13 Feb 2019
dates = datetime(datestr(dates));
%...
datenumber = datenum(dates);
The trip through datestr is unnecessary. And converting a datetime array to datenum is backward. datetime supports unique, and is much more practical than datenum.
Without an actual spreadsheet, we can only answer generically. Matlab has included tools for some time that help with dealing with time-based data. It looks like you're still using ancient tools ( datenum, xlsread, etc).
You should import your data with readtable, and convert it to a timetable (with table2timetable). Timetables have plenty of tools to deal with time, including retime which could be used to calculate daily/weekly/monthly/whatever statistics. It is very likely that splitting your original data into multiple table/arrays/whatever is not the right way to proceed.

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!