# Add date to a time data. Time data is from 7am to 7am.

2 views (last 30 days)
Kunal Tiwari on 19 Jan 2023
Commented: Star Strider on 29 Jan 2023
Hi.
I have an experimental data. The data set is being collected at sampling time of 7am on day one to 7am till next day.
The data is collected in excel. (.xls)
I want to plot the data from 7am Day 1, 12midnight, 7am Day 2 sequence.
When I plot it in matlab, it is being plotted from 00:00 to 24:00.
If I add a date, same date is added to entire data. Including the time after 2400.
desired result.
actual result.
Edit: Data added with the question.

Star Strider on 19 Jan 2023
Edited: Star Strider on 21 Jan 2023
It would help to have your Excel file, since I do not know if the time data are a duration array.
Try something like this —
Time = datetime('07:00', 'InputFormat','HH:mm', 'Format','HH:mm') + hours(0:23).';
Data = rand(size(Time));
T1 = table(Time,Data)
T1 = 24×2 table
Time Data _____ _________ 07:00 0.03685 08:00 0.53895 09:00 0.56889 10:00 0.45299 11:00 0.8689 12:00 0.000849 13:00 0.6309 14:00 0.12705 15:00 0.013077 16:00 0.70662 17:00 0.18835 18:00 0.11612 19:00 0.0067591 20:00 0.72767 21:00 0.4586 22:00 0.31414
writetable(T1, 'T1_Test.txt');
T1r.Time = datetime(T1r.Time, 'InputFormat','HH:mm')
T1r = 24×2 table
Time Data ____________________ _________ 21-Jan-2023 07:00:00 0.03685 21-Jan-2023 08:00:00 0.53895 21-Jan-2023 09:00:00 0.56889 21-Jan-2023 10:00:00 0.45299 21-Jan-2023 11:00:00 0.8689 21-Jan-2023 12:00:00 0.000849 21-Jan-2023 13:00:00 0.6309 21-Jan-2023 14:00:00 0.12705 21-Jan-2023 15:00:00 0.013077 21-Jan-2023 16:00:00 0.70662 21-Jan-2023 17:00:00 0.18835 21-Jan-2023 18:00:00 0.11612 21-Jan-2023 19:00:00 0.0067591 21-Jan-2023 20:00:00 0.72767 21-Jan-2023 21:00:00 0.4586 21-Jan-2023 22:00:00 0.31414
figure
plot(T1r.Time, T1r.Data)
grid
xtickformat('MMM-dd HH:mm')
title('Original')
Date = repmat(datetime('19-Jan-2023'), size(T1,1), 1); % Original Date
DI = cumsum([0; diff(hour(Time))<0]); % Day Increment
Date = Date + days(DI); % Incremented Date
DateTime = Date + timeofday(T1.Time); % Create New VAriable
T1 = removevars(T1,'Time') % Remove 'Time' (Optional)
T1 = 24×2 table
DateTime Data ____________________ _________ 19-Jan-2023 07:00:00 0.03685 19-Jan-2023 08:00:00 0.53895 19-Jan-2023 09:00:00 0.56889 19-Jan-2023 10:00:00 0.45299 19-Jan-2023 11:00:00 0.8689 19-Jan-2023 12:00:00 0.000849 19-Jan-2023 13:00:00 0.6309 19-Jan-2023 14:00:00 0.12705 19-Jan-2023 15:00:00 0.013077 19-Jan-2023 16:00:00 0.70662 19-Jan-2023 17:00:00 0.18835 19-Jan-2023 18:00:00 0.11612 19-Jan-2023 19:00:00 0.0067591 19-Jan-2023 20:00:00 0.72767 19-Jan-2023 21:00:00 0.4586 19-Jan-2023 22:00:00 0.31414
figure
plot(T1.DateTime, T1.Data)
grid
xtickformat('MMM-dd HH:mm')
title('Date Crorected')
EDIT — (21 Jan 2023 at 14:42)
Added writetable and readtable to simulate the actual data, and specific comparisons between the original and date-corrected arrays,
.
Kunal Tiwari on 29 Jan 2023
Hi.
I have sucessfull managed to get the data plotted now.
There is another improvement I need to make. I need to collect Time stamp from another file and plot lines to mark specific times in the data.
I tried your midnight line code, I am getting the following error.
Undefined function 'xline' for input arguments of type 'datetime'.
Star Strider on 29 Jan 2023
The xline function was introduced in R2018b. From the documentation section for the x input, datetime arguments are specifically permitted. It may be worthwhile to upgrade to R2022b (or R2023a, when it is released) if you have an earlier version.
One work-around could be:
plot([0 0]+datetime('25-Jan-2023 00:00:00'), ylim, '-r')
text(datetime('25-Jan-2023 00:00:00'), max(ylim), 'Midnight ', 'Horiz','right', 'Vert','top', 'Rotation',90, 'Color','r')
That will probably work in other versions. (It works in R2022b.)
.

### 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!