How do I get time in input?????
12 views (last 30 days)
Show older comments
t=input(' enter time ');and use t in time further in codes.....
0 Comments
Answers (3)
Blue
on 12 Nov 2021
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 Comments
Walter Roberson
on 12 Nov 2021
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 Comments
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!