Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 5 Aug. 2016
Bearbeitet: Azzi Abdelmalek am 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 Kommentar
Sandie Nhatien Vu
Sandie Nhatien Vu am 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?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by