MatLab Code for Time Calculation

From stgo
Jump to: navigation, search

Overview

This page contains a sample matlab code used to convert time of the data obtained from bear 272 into seconds

(Return to Home Range Calculation Manual.)

Main

%load in text files
data = load('Times.txt');
date = load ('Date.txt');
% 762 = number of rows of the data. this value will vary from bear to bear
seconds = zeros(762,1);
seconds2 = zeros(762,1);
DayOfYear = zeros (762,1);
for i = 1:762
   seconds (i,1) = ConvertToSec(data(i,1), data(i,2), data(i,3));% this conver hr-min-sec to seconds
   DayOfYear (i,1) = day(date(i,1), date(i,2), date(i,3));% calculate the number of day of the year
   seconds2 (i,1) = ConvertMD(DayOfYear (i,1), seconds (i,1));%convert month-day-hr-min-sec into seconds 
end

Function:ConverToSec

% This function will convert hr-min-secs into secs
function result = ConvertToSec(h,m,s)
result = s + m * 60 + h * 3600;

Function:day

function NumOfDay = day(Month, Day, Year)
%converting month and day into number of days
 if ((mod(Year, 4) == 0 && mod(Year,100)~= 0) || mod(Year,400) == 0)
     %This is leap year. Therefore Feb would have 29 days
       if Month == 1
            NumOfDay = Day;
       end %if month = 1
       if Month == 2
            NumOfDay = 31 + Day;
       end % if month = 2
       if Month == 3
            NumOfDay = 31 + 29 + Day;
       end % if month = 3
       if Month == 4
            NumOfDay = 31 * 2 + 29 + Day;
       end % if month = 4
       if Month == 5
           NumOfDay = 31 * 2 + 30 + 29 + Day;
       end % if month = 5
       if Month == 6
           NumOfDay = 31 * 3 + 30 + 29 + Day;
       end % if month = 6
       if Month == 7
           NumOfDay = 31 * 3 + 30 * 2 + 29 + Day;
       end % if month = 7
       if Month == 8
           NumOfDay = 31 * 4 + 30 * 2 + 29 + Day;
       end % if month = 8
       if Month == 9
           NumOfDay = 31 * 5 + 30 * 2 + 29 + Day;
       end % if month = 9
       if Month == 10
           NumOfDay = 31 * 5 + 30 * 3 + 29 + Day;
       end %if month = 10
       if Month == 11
           NumOfDay = 31 * 6 + 30 * 3 + 29 + Day;
       end %if month = 11
       if Month == 12
           NumOfDay = 31 * 6 + 30 * 4 + 29 + Day;
       end % if month = 12
 else 
       if Month == 1
            NumOfDay = Day;
       end %if month = 1
       if Month == 2
            NumOfDay = 31 + Day;
       end % if month = 2
       if Month == 3
            NumOfDay = 31 + 28 + Day;
       end % if month = 3
       if Month == 4
            NumOfDay = 31 * 2 + 28 + Day;
       end % if month = 4
       if Month == 5
           NumOfDay = 31 * 2 + 30 + 28 + Day;
       end % if month = 5
       if Month == 6
           NumOfDay = 31 * 3 + 30 + 28 + Day;
       end % if month = 6
       if Month == 7
           NumOfDay = 31 * 3 + 30 * 2 + 28 + Day;
       end % if month = 7
       if Month == 8
           NumOfDay = 31 * 4 + 30 * 2 + 28 + Day;
       end % if month = 8
       if Month == 9
           NumOfDay = 31 * 5 + 30 * 2 + 28 + Day;
       end % if month = 9
       if Month == 10
           NumOfDay = 31 * 5 + 30 * 3 + 28 + Day;
       end %if month = 10
       if Month == 11
           NumOfDay = 31 * 6 + 30 * 3 + 28 + Day;
       end %if month = 11
       if Month == 12
           NumOfDay = 31 * 6 + 30 * 4 + 28 + Day;
       end % if month = 12
 end %if function

Function:ConvertMD

%This Program will convert date (Month, day) and hour-min-sec into seconds
%This is done by first converting month and day into number of days (done in function called day) and
%then its converted into seconds, finally its added to the results
%produced from the first function
function Result2 = ConvertMD(NumOfDay, Result)
 Result2 = NumOfDay * 24 * 3600 + Result ;
end % function

--Cqiao 21:28, 21 June 2010 (UTC)