loop for clock each step is 20 min how to code this

1 view (last 30 days)
Hi everyone,
i have a problem coding for "clock", that means that the code should only go up to 60 min. and then start over. I know that I have to supply this with an "if-statement, that says if the minutes > 60 then it has to add 1 to the hour and subtract 60 from the minutes.. But how do i code this? How to define i as the hour and j as minutes?
My code so far:
for i = 13:0.2:20;
j = 13.16:0.2:20.16;
fprintf('The Train from London %.2f arrives in Campden at %.2f\n', i, j);
end
Output should look like this:
The Train from london 13.00 arrives in Campden at 13.16
The Train from london 13.20 arrives in Campden at 13.36
The Train from london 13.20 arrives in Campden at 13.56
. . .
The Train from london 20.00 arrives in Campden at 20.16
The train leaves every 20 min and takes 16 min to arrive.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 5 Aug 2016
Edited: Azzi Abdelmalek on 5 Aug 2016
Edit
a=datenum('13:00','HH:MM')
b=datenum('20:00','HH:MM')
jj=a:(1/(24*3)):b
date1=datestr(jj,'HH:MM')
date2=datestr(jj+1/(90),'HH:MM')
for k=1:size(date1,1)
fprintf('The Train from London %s arrives in Campden at %s\n', date1(k,:),date2(k,:));
end
  1 Comment
Sandie Nhatien Vu
Sandie Nhatien Vu on 5 Aug 2016
Thanks! Can l also do it by separate hour and minute in different variables and then supply with an "if-statement, that says if the minutes > 60 then it has to add 1 to the hour and subtract 60 from the minutes.. Or is the other way easier?

Sign in to comment.

More Answers (0)

Categories

Find more on Fortran with MATLAB 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!