# How to count hours using a loop?

17 views (last 30 days)
Karlo Korkees on 19 May 2020 at 23:53
Commented: Image Analyst on 20 May 2020 at 14:56
Im trying to write a code, that would count day-time hours and night-time hours using a loop? I dont't know where to start. Any help would be greatly appreciated.

Show 1 older comment
Karlo Korkees on 20 May 2020 at 3:19
What am trying to do is count how many hours are in the day and in the night. I'm using these required compoents: if/else or logical statement, loop and user input OR output.
For instance, I've used T = datetime('now','TimeZone','America/Los_Angeles'). But I don't think that gonna help me with my goal. Now I'm not sure if using a 24hour-time using: hoursspent = datestr([thetime{:}],'HH:MM' or T = datetime('now','TimeZone','America/Los_Angeles').
The if/else statement with logical relationship operators to narrow down the specific time values winthin the day/night. But I don't know how to incorporate AM/PM in Matlab. Iv'e been trying to use 'timezone' or 'clock' but I'm getting no where. Also using the loop in which will Indexes through each element of variable in the day/night.
dpb on 20 May 2020 at 12:41
Well, you still have to define what is "day" and what is "night" first...
Those requirements make sound like homework -- post the Q? as worded so we see the actual requirement.
Image Analyst on 20 May 2020 at 14:56
Can you use tic and toc?
startingTime = tic;
for k = 1 : 10 % lengthy, time consuming loop
% Time consuming code...
pause(2); % Whatever...
% See how long it took so far:
elapsedSeconds = toc(startingTime);
elapsedHours = elapsedSeconds / 3600;
fprintf('Elapsed hours = %.4f\n', elapsedHours)
end