Add date to a time data. Time data is from 7am to 7am.
2 views (last 30 days)
Show older comments
Kunal Tiwari on 19 Jan 2023
Commented: Star Strider on 29 Jan 2023
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.
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)
T1r = readtable('T1_Test.txt');
T1r.Time = datetime(T1r.Time, 'InputFormat','HH:mm')
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 = addvars(T1,DateTime,'Before','Data'); % Add It
T1 = removevars(T1,'Time') % Remove 'Time' (Optional)
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,
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.)
More Answers (0)
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!