Add date to a time data. Time data is from 7am to 7am.
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Star Strider
2023-1-19
编辑:Star Strider
2023-1-21
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)
writetable(T1, 'T1_Test.txt');
T1r = readtable('T1_Test.txt');
T1r.Time = datetime(T1r.Time, 'InputFormat','HH:mm')
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 = addvars(T1,DateTime,'Before','Data'); % Add It
T1 = removevars(T1,'Time') % Remove 'Time' (Optional)
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,
.
4 个评论
Star Strider
2023-1-29
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.)
.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!