Extracting Table Data by Date
Show older comments
I have a table full of data that eventually will be made to be frequently updated. I am currently using datestr to display the date as the first column in every row of data. Snippit of my code:
todayDate = datestr(now,'mm/dd/yyyy');
datesvct = repmat(todayDate, size(dataTake,1), 1);
dataTable = [ table(datesvct) dataTable];
Is it possible to reference and then extract information from the table from only the past week or the past 30 days and display it into a new table?
Accepted Answer
More Answers (1)
dpb
on 3 Sep 2015
0 votes
Yes, but it'll be easiest if you add a column that stores the datetime number (using late release) or the date number (if datetime type doesn't exist) and doing the selection via it rather than the string representation.
5 Comments
Andrew Smith
on 3 Sep 2015
dpb
on 3 Sep 2015
I'm at a little of a disadvantage as am limited to R2012b which predates both the table and datetime data types.
I presume the internal storage is still the date number despite the disply so you would simply use logical addressing on the column. If the storage is indeed the underlying value then logical addressing or ismember should work just as for any other numeric value.
Showing the precise code you used would help so at least can read the doc with a specific syntax; meanwhile mayhaps someone with a recent release will also chime in.
Andrew Smith
on 3 Sep 2015
Edited: dpb
on 3 Sep 2015
dpb
on 3 Sep 2015
Well, I'm more confused than ever...the above is "traditional" date numbers, not the new datetime class...a few comments--
- dateNumber as datenum(datestr(now)) is identical to just dateNumber=fix(now);
- I don't grok the usefulness in duplicating that same value 10 times?? What do you have in mind by doing this?
As for the selection if I do just do what you've done similarly except with a set of actual date values instead of a constant and supply another set of corollary data to go with it as
>> d=floor(now+[-4:4].'); % a series of dates beginning 4 days ago
>> datestr(d) % which specific dates are they?
ans =
30-Aug-2015
31-Aug-2015
01-Sep-2015
02-Sep-2015
03-Sep-2015
04-Sep-2015
05-Sep-2015
06-Sep-2015
07-Sep-2015
>> arry=[d rand(size(d))]; % make up the data to go with dates
>> ix=arry(:,1)>floor(now) % logical index-retrieve all after today
ix =
0
0
0
0
0
1
1
1
1
>> datestr(d(ix)) % see that above statement is really so...
ans =
04-Sep-2015
05-Sep-2015
06-Sep-2015
07-Sep-2015
>> dat=arry(ix,2) % and retrieve the data associated with those
dat =
0.7412
0.8547
0.5317
0.3931
>>
As example of ismember, presume want to return today only--other than the numerical test as above,
>> v=arry(ismember(arry(:,1),fix(now)),:) % get today's result
v =
1.0e+05 *
7.3621 0.0000
>> datestr(v(1)), v(2) % again, show that's what we really got
ans =
03-Sep-2015
ans =
0.7317
>>
Use the above ideas with the techniques for addressing table datasets (with dot indexing I believe?) and you should find joy...
Andrew Smith
on 4 Sep 2015
Categories
Find more on Calendar in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!