Converting Output from a Function

1 vue (au cours des 30 derniers jours)
Ashlee Rogers
Ashlee Rogers le 28 Oct 2020
Commenté : Ashlee Rogers le 28 Oct 2020
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?

Réponses (1)

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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by