MATLAB: Turning date strings to overal minutes

date arraysMATLABminute calculation

Hi, i have various Dat. files from which i calculate some variables, the files are named acoording to the date and time:
2019_12_31_17_43_31.39-Isd
2019_12_31_17_19_36.39-Ig
2019_12_31_16_55_41.38-Ig
2019_12_31_16_55_41.38-Isd
2020_1_1_12_7_5.13-Isd
where: filename = year_month_day_hour_min_sec. the problem is that the program doesnt do the standard YYYYMMDDHHMMSSSS depending on values (01 is given simply as 1)
i need to make a value-time plot for the variables, so i wanted to transform the overall date in to overal minutes starting from the lowest time.
any ideas or functions that could work?

Best Answer

Just use datetime, e.g.:
>> C = {...
'2019_12_31_17_43_31.39-Isd'
'2019_12_31_17_19_36.39-Ig'
'2019_12_31_16_55_41.38-Ig'
'2019_12_31_16_55_41.38-Isd'
'2020_1_1_12_7_5.13-Isd'
};
>> D = regexp(C,'^\w+\.\w+','match','once');
>> T = datetime(D, 'inputFormat','yyyy_M_d_H_m_s.SS')
T =
31-Dec-2019 17:43:31
31-Dec-2019 17:19:36
31-Dec-2019 16:55:41
31-Dec-2019 16:55:41
01-Jan-2020 12:07:05
and you can plot the datetime object directly (no need to count minutes from some random epoch):
>> V = rand(1,numel(T));
>> plot(T,V)
Notes:
  1. your timestamps are not in chronological order, the plot will show this too.
  2. converting to datetime has not made your fractional seconds disappear: the default 'format' simply does not show fractional seconds. Change the 'format' if you want to see them displayed.