How to convert time to a number?

30 ビュー (過去 30 日間)
James
James 2019 年 10 月 13 日
回答済み: Peter Perkins 2019 年 10 月 15 日
I want to use a prompted input in the format HH:SS and convert it to a number. For example, 17:15 (17 mins, 15 secs) would be 17.25. Is there a function in MATLAB that does this?
prompt =('Please enter your time in the format MM:SS: \n\n');
time = input(prompt);
reward = round(929187*time^(-3.235));

回答 (2 件)

prasanth s
prasanth s 2019 年 10 月 13 日
編集済み: prasanth s 2019 年 10 月 13 日
use following code to get values as string array.
time=input(prompt,'s');
split the output 'time' using split function with delimiter ':'
S=split(time,':');
then use following codes to convert the time
Easy way is: divide the seconds by 60 and add to minutes.
MATLAB way is:
D=minutes(17)+seconds(15);
M=minutes(D);

Peter Perkins
Peter Perkins 2019 年 10 月 15 日
>> d = duration('17:15','Format','mm:ss')
d =
duration
17:15
>> d.Format = 'm'
d =
duration
17.25 min

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by