How do I get time in input?????
22 ビュー (過去 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!