Add time column to matrix with measured values

10 views (last 30 days)
I have text file with two columns - temperature and humidity. I have read the file into MatLab matrix. How can I add date/time column to this matrix? Logging started 1/1/2017 13:00:00 and it logged after every 0.5 sec.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 1 Sep 2017
T_out = timetable(datetime(2017,1,1,13,0,.5*(0:size(M,1)-1)'),M);

More Answers (3)

KL
KL on 31 Aug 2017
Edited: KL on 31 Aug 2017
m_time = datetime('1/1/2017 13:00:00'):seconds(0.5):datetime('1/2/2017 13:00:00');
T = table(m_time',temperature,humidity);
or
TT = timetable(temperature,humidity,'RowTimes',m_time');
  4 Comments
KL
KL on 1 Sep 2017
MOVED Fred's Answer to Comment:
Thanks a lot! I got it working. But one thing - if I don't know the end time of measurement, I only have text file with N-rows of measured data (sometimes 100 values, sometimes 128909 values and so on..). Is it somehow possible to calculate the ending time based on text file rows?
m_time = datetime('1/1/2017 13:00:00'):seconds(0.5):datetime('YOUR END TIME HERE');
KL
KL on 1 Sep 2017
Edited: KL on 1 Sep 2017
You should be able to calculate the end time anyway. What's the size of matrix M? Let's say your matrix M has 'n' rows, then you should create m_time such that it gets n rows as well.
n = size(M,1);
end_time = datetime('1/1/2017 13:00:00.00','Format','MM/dd/yy HH:mm:ss.SS') + seconds(0.5)*(n-1); %EDIT: added format to display millisecond
m_time = (datetime('1/1/2017 13:00:00.00','Format','MM/dd/yy HH:mm:ss.SS'):seconds(0.5):end_time)'; %EDIT: added format to display millisecond
Then create the as I explained before and use the writetable.
Goodluck!

Sign in to comment.


Fred
Fred on 1 Sep 2017
Edited: Fred on 1 Sep 2017
Thanks a lot! I got it working. But one thing - if I don't know the end time of measurement, I only have text file with N-rows of measured data (sometimes 100 values, sometimes 128909 values and so on..). Is it somehow possible to calculate the ending time based on text file rows?
m_time = datetime('1/1/2017 13:00:00'):seconds(0.5):datetime('YOUR END TIME HERE');

Fred
Fred on 1 Sep 2017
Nice! Thank You!

Categories

Find more on Data Type Conversion 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!