How to convert hours, minutes, seconds to seconds?
    조회 수: 14 (최근 30일)
  
       이전 댓글 표시
    
Hi,
How can I return an array with
hh:min:sec
into
seconds?
For example
VarName2(1:5)
ans = 
    '14:54:25'
    '14:54:25'
    '14:54:25'
    '14:54:26'
    '14:54:26'
into a array with seconds?
댓글 수: 0
채택된 답변
  Daniel
      
 2014년 10월 16일
        Assuming you mean seconds in the day
    % initialize variables
    NUM_SECONDS_PER_DAY = 86400.0;
    timeStrings = {'14:54:25';'14:54:25';'14:54:25';'14:54:26';'14:54:26'};
    % convert times to fractional days using datenum
    timeFractionalDays = datenum(timeStrings);
    % leave only the part with the most recent day fraction
    timeDayFraction = mod(timeFractionalDays,1);
    % multiply by number of seconds in a day
    timeInSeconds = timeDayFraction .* NUM_SECONDS_PER_DAY
This produces the result
    timeInSeconds =
       1.0e+04 *
        5.3665
        5.3665
        5.3665
        5.3666
        5.3666
댓글 수: 8
  Daniel
      
 2014년 10월 16일
				Simply subtract the first value of the answer vector from itself.
    % initialize variables
    NUM_SECONDS_PER_DAY = 86400.0;
    % convert times to fractional days using datenum
    timeFractionalDays = datenum(VarName2);
    % leave only the part with the most recent day fraction
    timeDayFraction = mod(timeFractionalDays,1);
    % multiply by number of seconds in a day
    timeInSeconds = timeDayFraction .* NUM_SECONDS_PER_DAY;
    % find answer starting from first value
    timeInSeconds = timeInSeconds - timeInSeconds(1)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



