.I 12 Write a user-defined MATLAB function that determines the time elapsed betw
ID: 2082114 • Letter: #
Question
.I 12 Write a user-defined MATLAB function that determines the time elapsed between two events during a day. For the function name and arguments, use dt time diff(TA apl, TB,ap2). The input arguments to the function are: TA is a two-element vector with the time of the first event. The first element is the hour and the second element is the minute. apl is a string 'AM' or 'PM' which corresponds to the time of the first event. TB is a two-element vector with the time of the second event. The first element is the hour and the second element is the minute. ap2 is a string 'AM' or 'PM' which corresponds to the time of the second event. The output argument dt is a two-element vector with the time elapsed between two events. The first element is the number of hours and the second element is number of minutes. The function displays an emor message if the time entered for event Bis before the time entered for event A. Use the function to determine the time elapsed between the following events: (a) Event A: 5:37AM: Event B: 2:51PM. b) Event A: 12:53PM: Event B: 6:12PM. (c) Event A: 11:32PM: Event B: 3:18PM. or situation.) Answer dt 9 14 dt-5 19Explanation / Answer
Here I tried to find time difference by taking the difference between total number of minutes of two times.
Matlab script:
function dt = timediff(TA,ap1,TB,ap2)
if (TA(1)<12 && strcmp(ap1,'PM'))
temp1= TA(1)+12;%converting into 24 format zone
totmin1 = (temp1*60)+TA(2);%calculating total number of minutes
elseif (TA(1)==12 && strcmp(ap1,'AM'))
totmin1 = TA(2);%for 12:53 AM total number of minutes are 53
else
totmin1 = (TA(1)*60)+TA(2);
end
if (TB(1)<12 && strcmp(ap2,'PM'))
temp2=TB(1)+12;
totmin2 = (temp2*60)+TB(2);
elseif(TB(1)==12 && strcmp(ap2,'AM'))
totmin2 = TB(2);
else
totmin2 = (TB(1)*60)+TB(2);
end
if totmin1>totmin2
dt = 'Error: The time entered for event B is before the time entered for event A';
else
diff = totmin2-totmin1;
dt(1) = floor(diff/60);
dt(2) = rem(diff,60);
end
end
Command window log:
>> timediff([12 53],'PM',[6 12],'PM')
ans =
5 19
>> timediff([5 37],'AM',[2 51],'PM')
ans =
9 14
>> timediff([11 32],'PM',[3 18],'PM')
ans =
Error: The time entered for event B is before the time entered for event A
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.