Add time column to matrix with measured values

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.

 Risposta accettata

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

Più risposte (3)

KL
KL il 31 Ago 2017
Modificato: KL il 31 Ago 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 Commenti

Fred
Fred il 31 Ago 2017
Modificato: Fred il 31 Ago 2017
Thank you! But unfortunately I am quit beginner, so how exactly do I add time column to matrix?
M = dlmread('temp_hum.txt'); % Load text file.
M =
1 2
3 5
7 2
1 6
5 9
4 7
1 9
.....
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);
dlmwrite('temp_hum_time.txt',M, ';') % Save to text file.
You've only mentioned your measurement frequency. How long do you have the measurement from "1/1/2017 13:00:00"?
m_time = datetime('1/1/2017 13:00:00'):seconds(0.5):datetime('YOUR END TIME HERE');
Then you should transpose this and create the table with your M matrix.
T = table(m_time',M);
Then you could use writetable function,
writetable(T,'temp_hum_time_v1.txt','Delimiter',' ');
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 il 1 Set 2017
Modificato: KL il 1 Set 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!

Accedi per commentare.

Fred
Fred il 1 Set 2017
Modificato: Fred il 1 Set 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');

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Richiesto:

il 31 Ago 2017

Risposto:

il 1 Set 2017

Community Treasure Hunt

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

Start Hunting!

Translated by