필터 지우기
필터 지우기

Extract array information from date and create cell

조회 수: 2 (최근 30일)
Luke McLellan
Luke McLellan 2019년 2월 13일
댓글: Guillaume 2019년 2월 20일
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
Luke McLellan
Luke McLellan 2019년 2월 13일
iii = 1:32 is wrong. If all data was there then this would be the number of times per day.

댓글을 달려면 로그인하십시오.

채택된 답변

Luke McLellan
Luke McLellan 2019년 2월 19일
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
Luke McLellan
Luke McLellan 2019년 2월 20일
편집: Luke McLellan 2019년 2월 20일
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 2019년 2월 20일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
Guillaume 2019년 2월 13일
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.

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by