# Is there a way to convert times in this format to double, so I can perform mathematical operations on them?

조회 수: 2(최근 30일)
Teshan Rezel 2022년 1월 24일
댓글: Teshan Rezel 2022년 1월 24일
Hi folks,
I have an array of times that looks like this:
[1059, 1100, 1101...]
where 1059 = 59 minutes past 10am. I need to work out the elapsed number of minutes for each consequent element of the array. However, if I were to deduct 100-1059, I get 41 instead of 1.
Whilst there is a roundabout way of doing this, I was wondering if there's functionality in Matlab that will allow me to do this in a more elegant way?

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

### 채택된 답변

Steven Lord 2022년 1월 24일
How do you know that 100 represents 1 PM instead of 1 AM? Would it make more sense to adjust x so it includes 1300 instead of 100?
x = [1059, 1100, 1101, 100];
m = mod(x, 100);
h = (x-m)/100;
% Adjust h here using your rules so that 100 represents 1 PM instead of 1 AM
d = hours(h) + minutes(m);
d.Format = 'hh:mm'
d = 1×4 duration array
10:59 11:00 11:01 01:00
difference = diff(d)
difference = 1×3 duration array
00:01 00:01 -10:01
differenceInMinutes = minutes(difference)
differenceInMinutes = 1×3
1 1 -601
##### 댓글 수: 1표시숨기기 없음
Teshan Rezel 2022년 1월 24일
@Steven Lord fantastic, thank you!

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

### 추가 답변(1개)

Benjamin Thompson 2022년 1월 24일
MATLAB has a lot of date and time functions, as described by the article "Dates and Time" in the help documentation. If you are able to convert your four digit time codes into strings where hours and minutes are separated by a colon:
t = datetime({'10:59', '11:00', '11:01'},'Format','HH:mm')
t =
1×3 datetime array
10:59 11:00 11:01
>> dt = diff(t)
dt =
1×2 duration array
00:01:00 00:01:00

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

### 범주

Find more on Data Type Conversion in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by