Add time column to matrix with measured values
Afficher commentaires plus anciens
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
Plus de réponses (3)
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
KL
le 31 Août 2017
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',' ');
KL
le 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');
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!
Fred
le 1 Sep 2017
0 votes
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!