필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Sandie Nhatien Vu
Sandie Nhatien Vu 2016년 8월 5일
댓글: Sandie Nhatien Vu 2016년 8월 5일
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.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 5일
편집: Azzi Abdelmalek 2016년 8월 5일
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
Sandie Nhatien Vu
Sandie Nhatien Vu 2016년 8월 5일
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?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by