how to combine date and time

2 views (last 30 days)
Boss Man
Boss Man on 22 Feb 2020
Answered: Star Strider on 22 Feb 2020
So i have my data file attached
want to combine the data and time
then using the combined datetime produce different temptime graphs of diff days

Accepted Answer

Star Strider
Star Strider on 22 Feb 2020
In theory at least, datetime should be able to do this seamlessly using the timeofday function for the seconds. In practice, it cannot do it at all, at least when I tried it, so I resorted to datenum to do the necessary arithmetic to combine the dates and times.
Try this:
D = readtable('data.txt','ReadVariableNames',1);
DNS = datenum([zeros(size(D,1),5) D{:,2}]);
DND = datenum(compose('%s',D{:,1}),'dd/mm/yyyy');
DateTime = datetime(DND + DNS, 'ConvertFrom','datenum');
When I looked at the results with:
Check = DateTime([1:20:1000]);
it seemed to be converting them correctly. I may have gotten the days and months reversed, since the date format is not obvious. Correct that if necessary by changing ‘'dd/mm/yyyy'’ to ‘'mm/dd/yyyy'’.
The compose call takes a while, so in the interests of speed, save ‘DateTime’ to a .mat file to load quickly when you want to use it later.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!