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

1 回表示 (過去 30 日間)
Sharon
Sharon 2011 年 10 月 19 日
回答済み: Real User 2023 年 1 月 20 日
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!
  1 件のコメント
Sharon
Sharon 2011 年 10 月 19 日
Thanks. Will try the guide from the command as well

サインインしてコメントする。


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 Center および File ExchangeGet Started with MATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by