How do I get time in input?????
25 次查看(过去 30 天)
显示 更早的评论
t=input(' enter time ');and use t in time further in codes.....
0 个评论
回答(3 个)
Blue
2021-11-12
You mean like this ? You can enter time as '23:45' for example
prompt = 'What time is it ? ';
t = datetime(input(prompt), 'Format','HH:mm')
0 个评论
Walter Roberson
2021-11-12
You have several options:
- You can use the 's' option for input(), and get back a character vector from input(), which you then pass to duration() to parse
- You can use the 's' option for input() and get back a character vector from input(), which you then use something like sscanf() to parse into numeric components, that you then pass to duration() or build up using hours() + minutes() . Some older MATLAB had duration() but it was a few releases before duration() was able to parse character vectors
- You can use the 's' option for input() and get back a character vector from input(), which you then convert using textscan() with a '%T' format. It was a few releases of MATLAB before textscan() handled %T format.
- You can leave input() without the 's' option, but tell the user to enter a vector of numeric values, for which they themselves would have to enter a [ at the beginning and a ] at the end -- such as if the user had to enter [12 36] complete with the user entering the [ and ] .Then you pass them to duration()
- You cna leave input() without the 's' option, but tell the user to enter a ' or " delimited string, for which they themselves would have to enter the ' or " -- such as if the user had to enter '12:36' complete with the user entering the ' at the beginning and end. Then you pass the character vector or string scalar to duration(). It was a few release before duration() was able to parse character vectors
- Any of the above options involving character vectors or string scalars, but pass the input to datetime() with an 'InputFormat' option... and then take timeofday() of the result to get just the time component.
These days I would not hesitate to pass a character vector to duration()... but you did not happen to tell us which MATLAB release you are using, so I cannot assume that your release is able to handle character vectors for duration()
0 个评论
另请参阅
类别
在 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!