Inserting Timestamp into a Matrix

7 views (last 30 days)
Megan Stapley
Megan Stapley on 21 Mar 2019
Answered: Peter Perkins on 22 Mar 2019
Hi all,
The first column of my matrix is a timestamp. How do I get it in a normal date and time format rather than this?
  4 Comments
Star Strider
Star Strider on 21 Mar 2019
What are the values that are not ‘2019’? Do they have any significance?
Megan Stapley
Megan Stapley on 21 Mar 2019
They are a measurement of data at that time stamp.

Sign in to comment.

Answers (2)

Star Strider
Star Strider on 21 Mar 2019
I still have no idea what you want to do.
However, if you want to concatenate the even rows to the odd rows and have ‘2019’ be the first column in each row, this works:
A = [2019 -12
0.5 0
2019 -12
1 0
2019 -12
1.5 0];
B = [A(1:2:end,:) A(2:2:end,:)]
producing:
B =
2019 -12 0.5 0
2019 -12 1 0
2019 -12 1.5 0
If all you have is the year in your time stamp, I would just leave it as is. There is not real benefit to converting it to a datenum or datetime value. However if you absolutely must, this works:
Y = datetime(num2str(B(:,1)), 'InputFormat','yyyy','Format','yyyy');
T = table(Y,B(:,2),B(:,3),B(:,4)) % Table
producing:
T =
3×4 table
Y Var2 Var3 Var4
____ ____ ____ ____
2019 -12 0.5 0
2019 -12 1 0
2019 -12 1.5 0
Experiment to get the result you want.

Peter Perkins
Peter Perkins on 22 Mar 2019
I'm gonna take a wild guess, because there's very little to go on here.
I'm gonna guess you have format short, and you have a matrix each of whose rows is like this
>> x = [2019.012345678 -12 .5 0]
x =
1.0e+03 *
2.0190 -0.0120 0.0005 0
I may be wrong. What follows the "2019." may be somethign else. Who knows?
The first thing is, you can't put "time" and "numbers" together in a numeric matrix unless your time is represented as a number, and you already have that. So as Star Strider suggests, you want a table.
>> X = [2019+rand(5,1) randn(5,1)]
X =
1.0e+03 *
2.0191 0.0010
2.0192 0.0005
2.0192 -0.0000
2.0194 -0.0000
2.0190 -0.0008
>> t = array2table(X)
t =
5×2 table
X1 X2
______ _________
2019.1 0.96423
2019.2 0.52006
2019.2 -0.020028
2019.4 -0.034771
2019 -0.79816
>> t.Year = floor(t.X1);
>> t.YearFrac = t.X1 - t.Year
t =
5×4 table
X1 X2 Year YearFrac
______ _________ ____ ________
2019.1 0.96423 2019 0.12332
2019.2 0.52006 2019 0.18391
2019.2 -0.020028 2019 0.23995
2019.4 -0.034771 2019 0.41727
2019 -0.79816 2019 0.049654
>> t.Time = datetime(t.Year,1,1) + years(t.YearFrac)
t =
5×5 table
X1 X2 Year YearFrac Time
______ _________ ____ ________ ____________________
2019.1 0.96423 2019 0.12332 15-Feb-2019 00:59:29
2019.2 0.52006 2019 0.18391 09-Mar-2019 04:06:09
2019.2 -0.020028 2019 0.23995 29-Mar-2019 15:22:50
2019.4 -0.034771 2019 0.41727 02-Jun-2019 09:41:16
2019 -0.79816 2019 0.049654 19-Jan-2019 03:15:42
>> tt = table2timetable(t(:,{'Time' 'X2'}))
tt =
5×1 timetable
Time X2
____________________ _________
15-Feb-2019 00:59:29 0.96423
09-Mar-2019 04:06:09 0.52006
29-Mar-2019 15:22:50 -0.020028
02-Jun-2019 09:41:16 -0.034771
19-Jan-2019 03:15:42 -0.79816

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!