Merge date row and time column to a single datetime row
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, I have some data that has been organised with each column a different time of day and each row a diffrent day of the year.
Is there some way to reorganise this table or array in such a way that the date and times have been merged into one column?
Below is a small subset of the data with the date columns and time rows.
Attached is a slightly larger array of the same data with out the time and date headings

Date 00:30 01:00 01:30 02:00 02:30 03:00 03:30 04:00 04:30 05:00
01/01/2019 1.9 1.6 1.4 1.6 1.6 1.5 1.4 1.6 1.1 1.9
02/01/2019 2 1.7 1.5 1.6 1.5 1.6 1.3 1.9 1.5 1.6
03/01/2019 2 1.6 1.5 1.7 1.5 1.5 1.2 1.9 1.7 1.6
04/01/2019 1.9 1.3 2 1.7 1.5 1.7 1.7 1.6 1.4 2
05/01/2019 1.5 1.8 1.6 1.6 1.7 1.6 1.8 1.8 1.7 1.8
06/01/2019 1.4 1.9 1.6 1.6 1.6 1.5 1.8 1.6 1.5 1.7
07/01/2019 1.6 1.6 1.5 1.5 1.6 1.5 1.6 1.6 1.4 1.6
2 commentaires
dpb
le 12 Juil 2021
That data file looks nothing like the sample including no dates nor times... and 48 columns although I suppose that could be 24 hrs * 2 columns/hour?
dpb
le 12 Juil 2021
If, in fact, that is the data, then just build the datetime array from first day in the file to the last and the data are simply
data=reshape(data.',[],1);
Réponses (1)
Cris LaPierre
le 12 Juil 2021
You mention the mat file contains the data without the dates or times, correct? I think you can get to what you want fairly simply using implicit expansion and linear indexing. Keep in mind that MATLAB uses Column-Major ordering by default.
load 'example data.mat'
% Create time and date arrays the size of data
tm = hours(0.5):minutes(30):hours(size(show,2)/2);
date = datetime(2019,1,1):calmonths(1):datetime(2019,size(show,1),1);
% combine dates and times. Transpose so columns are months
dtm = (date' + tm)';
data = show';
% use linear indexing to create vectors of dates and data
dataTT = timetable(dtm(:),data(:))
0 commentaires
Voir également
Catégories
En savoir plus sur Time Series Objects dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!