Any workaround to convert duration (seconds, hours, etc...) to a numeric value (double)

 채택된 답변

Steven Lord
Steven Lord 2022년 2월 28일

3 개 추천

Use seconds, hours, etc. depending on how you want the duration array to be converted to a number.
d = duration(1, 2, 3)
d = duration
01:02:03
s = seconds(d)
s = 3723
m = minutes(d)
m = 62.0500
h = hours(d)
h = 1.0342

댓글 수: 4

Camilo G.
Camilo G. 2022년 3월 2일
what i really looking its for the numerical value as "double" data type. By using the suggested functions im still getting the output as a "duration" data type.
Walter Roberson
Walter Roberson 2022년 3월 2일
No, those outputs s, m, h are double() not duration()
If the input to seconds() is a double then it constructs a duration() of that many seconds long.
If the input to seconds() is a duration then it examines the duration information and converts units to seconds as a double()
eko supriyadi
eko supriyadi 2022년 10월 17일
and how to convert duration 901 days to 901 (numeric only)?
When you apply minutes() or seconds() or days() or hours() to a double, then a duration object is created.
When you apply those functions to duration objects, double values are created.
For example
hours(days(7.2))

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

추가 답변 (1개)

Korosh Agha Mohammad Ghasemi
Korosh Agha Mohammad Ghasemi 2024년 6월 25일
이동: Voss 2024년 6월 25일

0 개 추천

To convert a duration object to a numeric value (double) in MATLAB, you can use functions like seconds, minutes, hours, etc., depending on the units you want to convert to. These functions will convert the duration to a numeric value representing the number of seconds, minutes, or hours, respectively.
Here is an example of how to do this:
% Create a duration object
d = duration(1, 2, 3); % 1 hour, 2 minutes, 3 seconds
% Convert the duration to numeric values in different units
secondsValue = seconds(d); % Convert to seconds
minutesValue = minutes(d); % Convert to minutes
hoursValue = hours(d); % Convert to hours
% Display the results
disp(['Duration in seconds: ', num2str(secondsValue)]);
disp(['Duration in minutes: ', num2str(minutesValue)]);
disp(['Duration in hours: ', num2str(hoursValue)]);
In this example:
  • seconds(d) converts the duration to seconds and returns it as a double value.
  • minutes(d) converts the duration to minutes.
  • hours(d) converts the duration to hours.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2022년 2월 28일

이동:

2024년 6월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by