필터 지우기
필터 지우기

Converting Output from a Function

조회 수: 1 (최근 30일)
Ashlee Rogers
Ashlee Rogers 2020년 10월 28일
댓글: Ashlee Rogers 2020년 10월 28일
I have a script which has the user input 2 time intervals and then calls a function to add those time intervals together (adds the hours, minutes, and seconds separately). I wrote several IF statements to make it so that if 60 seconds were entered or 60 minutes was entered, it would convert to 1 minute or 1 hour, respectively. However, if I enter 60 seconds AND 60 minutes, the output only converts one of these. So, I am trying to figure out how I can perform the IF statement for the seconds, first, and then update the total of the minutes and seconds AND THEN do another IF statement for the minutes and update the total of the hours and minutes after based on what I have written already.
I have the following code so far:
function [hmsTotal] = intervalAddition(h1,min1,s1,h2,min2,s2)
% This function adds the first and second time intervals input by the
% user. The equation is named hmsTotal.
hoursTotal = (h1 + h2); %sum of the hours input by the user
minutesTotal = (min1 + min2); %sum of the minutes input by the user
secondsTotal = (s1 + s2); %sum of the seconds input by the user
%if statement which converts seconds to minutes if seconds is greater than
%or equal to 60 and converts minutes to hours if minutes is greater than or
%equal to 60.
if secondsTotal == 60
hmsTotal = [hoursTotal (minutesTotal + 1) (secondsTotal - 60)];
elseif secondsTotal > 60
hmsTotal = [hoursTotal (minutesTotal + fix(secondsTotal ./ 60)) fix(secondsTotal ./ 60 - 1 + rem(secondsTotal,60))];
elseif minutesTotal == 60
hmsTotal = [(hoursTotal + 1) (minutesTotal - 60) secondsTotal];
elseif minutesTotal > 60
hmsTotal = [(hoursTotal + fix(minutesTotal ./ 60)) fix(minutesTotal ./ 60 - 1 + rem(minutesTotal,60)) secondsTotal];
else
hmsTotal = [hoursTotal minutesTotal secondsTotal];
end
end
How can I change what I have to make this work?

답변 (1개)

David Hill
David Hill 2020년 10월 28일
Recommend you look at datetime() function. You can easily add two datetimes together.
  댓글 수: 1
Ashlee Rogers
Ashlee Rogers 2020년 10월 28일
Yes, Ive seen that function before, but I am required to do this with IF statements, unfortunately. Its for an intro-level course.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by