Merge date row and time column to a single datetime row

9 vues (au cours des 30 derniers jours)
Samir Soares
Samir Soares le 12 Juil 2021
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
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
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);

Connectez-vous pour commenter.

Réponses (1)

Cris LaPierre
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(:))
dataTT = 288×1 timetable
Time Var1 ____________________ ______ 01-Jan-2019 00:30:00 2358.2 01-Jan-2019 01:00:00 3396.6 01-Jan-2019 01:30:00 4456.1 01-Jan-2019 02:00:00 3397.2 01-Jan-2019 02:30:00 3414 01-Jan-2019 03:00:00 3427.1 01-Jan-2019 03:30:00 2305.6 01-Jan-2019 04:00:00 3395.7 01-Jan-2019 04:30:00 3895.3 01-Jan-2019 05:00:00 2752 01-Jan-2019 05:30:00 2270.8 01-Jan-2019 06:00:00 2277.6 01-Jan-2019 06:30:00 2255.8 01-Jan-2019 07:00:00 3346.8 01-Jan-2019 07:30:00 3412.2 01-Jan-2019 08:00:00 2917.8

Catégories

En savoir plus sur Time Series Objects dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by