필터 지우기
필터 지우기

Time difference between two sets of times in hrs mins and seconds.. GUI if possible

조회 수: 2 (최근 30일)
Hi im new to MATLAB and trying to setup a GUI to calculate the time difference between two sets of times. Eg: 10:00:00 and 11:01:00
Difference would be 1 hr 1min and 0 seconds..
thanks in advance

채택된 답변

Thomas
Thomas 2011년 10월 19일
Hi Sharon, I have written a code for this a long time ago. Hope this helps..
close all
% Prompt to enter the time using GUI
prompt ={'Enter Start time and End time:'};
dlg_title = 'Time difference calculator';
num_lines=1;
def={'0 0 0 1 1 1'};
str1 = inputdlg(prompt,dlg_title,num_lines,def);
time1=str2num(str1{1});
% get the times
time1hr=time1(1);
time1min=time1(2);
time1sec=time1(3);
time2hr=time1(4);
time2min=time1(5);
time2sec=time1(6);
time1out=(time1hr*60*60)+(time1min*60)+time1sec;
time2out=(time2hr*60*60)+(time2min*60)+time2sec;
% actual calculation
diffInSec=time2out-time1out
diffInMin=diffInSec/60
if diffInMin >60
Hrs=(diffInMin/60)-mod(diffInMin/60,1)
minutes=diffInMin-(Hrs*60)-mod(diffInMin,1)
seconds=diffInSec-(Hrs*60*60)-(minutes*60)
else
afterdecimal=mod(diffInMin,1);
minutes=diffInMin-afterdecimal
seconds=diffInSec-(minutes*60)
end
  댓글 수: 1
Sharon
Sharon 2011년 10월 19일
Thanks for the quick reply. This was what I needed. Wasn't sure how much time a reply would take.

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

추가 답변 (2개)

Sean de Wolski
Sean de Wolski 2011년 10월 19일
So start by opening GUIDE
guide %at the command line
Then draw the GUI so that it has edit boxes to enter times, static boxes to give directions, and a push button to press to give you the times. After that, write the functions to do the work. Or write the functions to do the work first and then add them to a GUI. Ask specific detailed MATLAB questions here when you get stuck.
Welcome to MATLAB Answers!

Real User
Real User 2023년 1월 20일
>> duration("11:01:00")-duration("10:00:00")
ans =
duration
01:01:00
>> string(duration("11:01:00")-duration("10:00:00"))
ans =
"01:01:00"
>> [h, m, s]=hms(duration("11:01:00")-duration("10:00:00"))
h =
1
m =
1
s =
0
>> d = duration(h,m,s)
d =
duration
01:01:00
These work even if you include the dates.
[m,d,t] = split(T,{'months','days','time'})

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by