How do I write time vector starting from midnight (12:00 am) hourly to 24th hour using time-intervals of 6 minutes (tenths of an hour).

12 views (last 30 days)
I have to declare two variables: time and P(t), how do I do it?
Time is already explained in my question and P(t) has different values for different times for example:
For t<6, P(t)=0
Like how do I start my code I do not undertand

Answers (1)

Vaibhav
Vaibhav on 9 Feb 2024
Edited: Vaibhav on 9 Feb 2024
Hi Maha
You can create a time vector that starts from midnight and increments every 6 minutes until the 24th hour using the "datetime" and "minutes" functions. You can then use logical indexing or a loop to define the variable "P(t)" based on the conditions you've provided.
You can refer the code snippet below:
% Define the start and end times
startTime = datetime('today', 'Format', 'dd-MMM-yyyy HH:mm:ss');
endTime = startTime + days(1);
% Create a time vector with 6-minute intervals
time = startTime:minutes(6):endTime;
% Preallocate P(t) with zeros
P = zeros(size(time));
% Define P(t) based on the condition
for i = 1:length(time)
t = hours(time(i) - startTime); % Convert time to hours since midnight
if t < 6
P(i) = 0; % For t < 6, P(t) = 0
else
% Add other conditions for P(t) here
% For example, if you have other conditions for different time ranges,
% you would add them in this section using elseif or else statements.
end
end
% Note: The time vector includes the 24th hour (midnight of the next day),
% if you want to exclude it, you can use:
% time = startTime:minutes(6):(endTime - minutes(1));
time
time = 1×241 datetime array
Columns 1 through 8 09-Feb-2024 00:00:00 09-Feb-2024 00:06:00 09-Feb-2024 00:12:00 09-Feb-2024 00:18:00 09-Feb-2024 00:24:00 09-Feb-2024 00:30:00 09-Feb-2024 00:36:00 09-Feb-2024 00:42:00 Columns 9 through 16 09-Feb-2024 00:48:00 09-Feb-2024 00:54:00 09-Feb-2024 01:00:00 09-Feb-2024 01:06:00 09-Feb-2024 01:12:00 09-Feb-2024 01:18:00 09-Feb-2024 01:24:00 09-Feb-2024 01:30:00 Columns 17 through 24 09-Feb-2024 01:36:00 09-Feb-2024 01:42:00 09-Feb-2024 01:48:00 09-Feb-2024 01:54:00 09-Feb-2024 02:00:00 09-Feb-2024 02:06:00 09-Feb-2024 02:12:00 09-Feb-2024 02:18:00 Columns 25 through 32 09-Feb-2024 02:24:00 09-Feb-2024 02:30:00 09-Feb-2024 02:36:00 09-Feb-2024 02:42:00 09-Feb-2024 02:48:00 09-Feb-2024 02:54:00 09-Feb-2024 03:00:00 09-Feb-2024 03:06:00 Columns 33 through 40 09-Feb-2024 03:12:00 09-Feb-2024 03:18:00 09-Feb-2024 03:24:00 09-Feb-2024 03:30:00 09-Feb-2024 03:36:00 09-Feb-2024 03:42:00 09-Feb-2024 03:48:00 09-Feb-2024 03:54:00 Columns 41 through 48 09-Feb-2024 04:00:00 09-Feb-2024 04:06:00 09-Feb-2024 04:12:00 09-Feb-2024 04:18:00 09-Feb-2024 04:24:00 09-Feb-2024 04:30:00 09-Feb-2024 04:36:00 09-Feb-2024 04:42:00 Columns 49 through 56 09-Feb-2024 04:48:00 09-Feb-2024 04:54:00 09-Feb-2024 05:00:00 09-Feb-2024 05:06:00 09-Feb-2024 05:12:00 09-Feb-2024 05:18:00 09-Feb-2024 05:24:00 09-Feb-2024 05:30:00 Columns 57 through 64 09-Feb-2024 05:36:00 09-Feb-2024 05:42:00 09-Feb-2024 05:48:00 09-Feb-2024 05:54:00 09-Feb-2024 06:00:00 09-Feb-2024 06:06:00 09-Feb-2024 06:12:00 09-Feb-2024 06:18:00 Columns 65 through 72 09-Feb-2024 06:24:00 09-Feb-2024 06:30:00 09-Feb-2024 06:36:00 09-Feb-2024 06:42:00 09-Feb-2024 06:48:00 09-Feb-2024 06:54:00 09-Feb-2024 07:00:00 09-Feb-2024 07:06:00 Columns 73 through 80 09-Feb-2024 07:12:00 09-Feb-2024 07:18:00 09-Feb-2024 07:24:00 09-Feb-2024 07:30:00 09-Feb-2024 07:36:00 09-Feb-2024 07:42:00 09-Feb-2024 07:48:00 09-Feb-2024 07:54:00 Columns 81 through 88 09-Feb-2024 08:00:00 09-Feb-2024 08:06:00 09-Feb-2024 08:12:00 09-Feb-2024 08:18:00 09-Feb-2024 08:24:00 09-Feb-2024 08:30:00 09-Feb-2024 08:36:00 09-Feb-2024 08:42:00 Columns 89 through 96 09-Feb-2024 08:48:00 09-Feb-2024 08:54:00 09-Feb-2024 09:00:00 09-Feb-2024 09:06:00 09-Feb-2024 09:12:00 09-Feb-2024 09:18:00 09-Feb-2024 09:24:00 09-Feb-2024 09:30:00 Columns 97 through 104 09-Feb-2024 09:36:00 09-Feb-2024 09:42:00 09-Feb-2024 09:48:00 09-Feb-2024 09:54:00 09-Feb-2024 10:00:00 09-Feb-2024 10:06:00 09-Feb-2024 10:12:00 09-Feb-2024 10:18:00 Columns 105 through 112 09-Feb-2024 10:24:00 09-Feb-2024 10:30:00 09-Feb-2024 10:36:00 09-Feb-2024 10:42:00 09-Feb-2024 10:48:00 09-Feb-2024 10:54:00 09-Feb-2024 11:00:00 09-Feb-2024 11:06:00 Columns 113 through 120 09-Feb-2024 11:12:00 09-Feb-2024 11:18:00 09-Feb-2024 11:24:00 09-Feb-2024 11:30:00 09-Feb-2024 11:36:00 09-Feb-2024 11:42:00 09-Feb-2024 11:48:00 09-Feb-2024 11:54:00 Columns 121 through 128 09-Feb-2024 12:00:00 09-Feb-2024 12:06:00 09-Feb-2024 12:12:00 09-Feb-2024 12:18:00 09-Feb-2024 12:24:00 09-Feb-2024 12:30:00 09-Feb-2024 12:36:00 09-Feb-2024 12:42:00 Columns 129 through 136 09-Feb-2024 12:48:00 09-Feb-2024 12:54:00 09-Feb-2024 13:00:00 09-Feb-2024 13:06:00 09-Feb-2024 13:12:00 09-Feb-2024 13:18:00 09-Feb-2024 13:24:00 09-Feb-2024 13:30:00 Columns 137 through 144 09-Feb-2024 13:36:00 09-Feb-2024 13:42:00 09-Feb-2024 13:48:00 09-Feb-2024 13:54:00 09-Feb-2024 14:00:00 09-Feb-2024 14:06:00 09-Feb-2024 14:12:00 09-Feb-2024 14:18:00 Columns 145 through 152 09-Feb-2024 14:24:00 09-Feb-2024 14:30:00 09-Feb-2024 14:36:00 09-Feb-2024 14:42:00 09-Feb-2024 14:48:00 09-Feb-2024 14:54:00 09-Feb-2024 15:00:00 09-Feb-2024 15:06:00 Columns 153 through 160 09-Feb-2024 15:12:00 09-Feb-2024 15:18:00 09-Feb-2024 15:24:00 09-Feb-2024 15:30:00 09-Feb-2024 15:36:00 09-Feb-2024 15:42:00 09-Feb-2024 15:48:00 09-Feb-2024 15:54:00 Columns 161 through 168 09-Feb-2024 16:00:00 09-Feb-2024 16:06:00 09-Feb-2024 16:12:00 09-Feb-2024 16:18:00 09-Feb-2024 16:24:00 09-Feb-2024 16:30:00 09-Feb-2024 16:36:00 09-Feb-2024 16:42:00 Columns 169 through 176 09-Feb-2024 16:48:00 09-Feb-2024 16:54:00 09-Feb-2024 17:00:00 09-Feb-2024 17:06:00 09-Feb-2024 17:12:00 09-Feb-2024 17:18:00 09-Feb-2024 17:24:00 09-Feb-2024 17:30:00 Columns 177 through 184 09-Feb-2024 17:36:00 09-Feb-2024 17:42:00 09-Feb-2024 17:48:00 09-Feb-2024 17:54:00 09-Feb-2024 18:00:00 09-Feb-2024 18:06:00 09-Feb-2024 18:12:00 09-Feb-2024 18:18:00 Columns 185 through 192 09-Feb-2024 18:24:00 09-Feb-2024 18:30:00 09-Feb-2024 18:36:00 09-Feb-2024 18:42:00 09-Feb-2024 18:48:00 09-Feb-2024 18:54:00 09-Feb-2024 19:00:00 09-Feb-2024 19:06:00 Columns 193 through 200 09-Feb-2024 19:12:00 09-Feb-2024 19:18:00 09-Feb-2024 19:24:00 09-Feb-2024 19:30:00 09-Feb-2024 19:36:00 09-Feb-2024 19:42:00 09-Feb-2024 19:48:00 09-Feb-2024 19:54:00 Columns 201 through 208 09-Feb-2024 20:00:00 09-Feb-2024 20:06:00 09-Feb-2024 20:12:00 09-Feb-2024 20:18:00 09-Feb-2024 20:24:00 09-Feb-2024 20:30:00 09-Feb-2024 20:36:00 09-Feb-2024 20:42:00 Columns 209 through 216 09-Feb-2024 20:48:00 09-Feb-2024 20:54:00 09-Feb-2024 21:00:00 09-Feb-2024 21:06:00 09-Feb-2024 21:12:00 09-Feb-2024 21:18:00 09-Feb-2024 21:24:00 09-Feb-2024 21:30:00 Columns 217 through 224 09-Feb-2024 21:36:00 09-Feb-2024 21:42:00 09-Feb-2024 21:48:00 09-Feb-2024 21:54:00 09-Feb-2024 22:00:00 09-Feb-2024 22:06:00 09-Feb-2024 22:12:00 09-Feb-2024 22:18:00 Columns 225 through 232 09-Feb-2024 22:24:00 09-Feb-2024 22:30:00 09-Feb-2024 22:36:00 09-Feb-2024 22:42:00 09-Feb-2024 22:48:00 09-Feb-2024 22:54:00 09-Feb-2024 23:00:00 09-Feb-2024 23:06:00 Columns 233 through 240 09-Feb-2024 23:12:00 09-Feb-2024 23:18:00 09-Feb-2024 23:24:00 09-Feb-2024 23:30:00 09-Feb-2024 23:36:00 09-Feb-2024 23:42:00 09-Feb-2024 23:48:00 09-Feb-2024 23:54:00 Column 241 10-Feb-2024 00:00:00
You can refer to the MathWorks documentation links below to learn more about "datetime" and minutes functions:
Hope this helps!
  3 Comments
Walter Roberson
Walter Roberson on 9 Feb 2024
No, your resulting t will be in hours, but none of MATLAB's time systems run in hours. The old MATLAB time system runs in days (since January 1, 0000), and the newer MATLAB time system uses objects (that are internally a pair of 64 bit integers) to represent time.
With the newer MATLAB time system, you have your choice between absolute times ( datetime objects) and relative times ( duration objects). @Vaibhav already showed absolute times. For durations it would look like
t = hours(0) : minutes(6) : hours(24);

Sign in to comment.

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!