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.

 Réponse acceptée

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

Plus de réponses (3)

KL
KL le 31 Août 2017
Modifié(e) : KL le 31 Août 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 commentaires

Fred
Fred le 31 Août 2017
Modifié(e) : Fred le 31 Août 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 le 1 Sep 2017
Modifié(e) : KL le 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!

Connectez-vous pour commenter.

Fred
Fred le 1 Sep 2017
Modifié(e) : Fred le 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');

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by