Creating time index in timetable
28 views (last 30 days)
Show older comments
Newer to MATLAB (oringally a Python user) and I am trying to create a table of timeseries water level data and it is proving rather difficult. (Pandas dataframes are 100x easier imo).
So, I have a indexVal = 1x2088 datetime and I am trying to insert it as the row time in a new empty time table that I can then fill with water level values which are of format data = 1x2088 cell. How do I do this? I have tried a few things :
TT = timetable(indexVal);
convert indexVal -> Dates = {indexVal}; then repeat above with dates
Is there a way to create a timetable with just the dates in the first column before adding any data?
I was originally going to create a table and got it or so I thought, but it fills them horizontally instead of vertically and I couldn't get that to transpose correctly.
Any help and advice would be appreciated.
WANT
Time Var1 Var2
------------------------------ | ----- | ----- |
2016-01-05 01:00:00 ( Var columns empty to start)
2016-01-05 02:00:00
2016-01-05 03:00:00
2016-01-05 04:00:00
2016-01-05 05:00:00
THEN append to corresponding var
data = {[0.4857]} {[0.5367]} {[0.5771]} {[0.5969]} {[0.5957]} {[0.5789]} {[0.5485]} {[0.4969]} {[0.4281]} {[0.3488]} {[0.2699]} {[0.1999]} {[0.1405]} {[0.1002]} {[0.0683]} {[0.0454]} {[0.0480]} {[0.0559]} {[0.0832]} {[0.1308]} {[0.1771]} {[0.2296]} {[0.2855]} {[0.3482]} {[0.4167]} {[0.4910]} {[0.5623]} {[0.6185]} {[0.6586]} {[0.6803]} {[0.6805]} {[0.6620]} {[0.6230]} {[0.5631]} {[0.4885]} {[0.4129]} {[0.3437]} ...equivalent length as time
0 Comments
Answers (1)
Peter Perkins
on 7 Mar 2022
Autumn, "indexVal = 1x2088" is the problem. Tables are more general than data frames in Python, in that a variable in a table may itself have multiple columns (which is why a table contains "variables", not "columns"). So when you make a table from a 1x2088 variable in your workspace, you get a 1x1 table whose only variable has 2088 length-inbe columns. Ditto for timetables.
On the other hand, the RowTime parameter accepts any vector. So, these are equivalent:
>> tt = timetable('RowTimes',datetime(2022,3,7:10))
tt =
4×0 empty timetable
>> tt = timetable(datetime(2022,3,7:10)')
tt =
4×0 empty timetable
and then add data as you wish:
>> tt.X = [1;2;3;4]
tt =
4×1 timetable
Time X
___________ _
07-Mar-2022 1
08-Mar-2022 2
09-Mar-2022 3
10-Mar-2022 4
>> tt.Y = ["one";"two";"three";"four"]
tt =
4×2 timetable
Time X Y
___________ _ _______
07-Mar-2022 1 "one"
08-Mar-2022 2 "two"
09-Mar-2022 3 "three"
10-Mar-2022 4 "four"
Or even
>> [tt array2table(rand(4,2))]
ans =
4×4 timetable
Time X Y Var1 Var2
___________ _ _______ ________ _______
07-Mar-2022 1 "one" 0.82119 0.64912
08-Mar-2022 2 "two" 0.015403 0.73172
09-Mar-2022 3 "three" 0.043024 0.64775
10-Mar-2022 4 "four" 0.16899 0.45092
Also, not sure how you ended up with it, but you really don't want to have
data = {[0.4857]} {[0.5367]} {[0.5771]} {[0.5969]} ...
That's a cell array containing scalar numeric. You want a double vector. You should figure out how you got that, but for now cell2num will get you out of there.
2 Comments
Peter Perkins
on 9 Mar 2022
What I said is that you should not be using row vectors, you should be using column vectors. You may simply need to transpose your vectors.
x = 1:5 is a row vector. x = (1:5)' is a column vector.
You have said several times that what you have done isn't working, but you have not shown what you have done. Hard to give specific advice, but nothing you have described should be at all difficult or complicated. For example, what went wrong that led you to use num2cell?
See Also
Categories
Find more on Language Fundamentals 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!