How do I get time in input?????

12 views (last 30 days)
Jatin Chaturvedi
Jatin Chaturvedi on 12 Nov 2021
Answered: Walter Roberson on 12 Nov 2021
t=input(' enter time ');and use t in time further in codes.....

Answers (3)

Fangjun Jiang
Fangjun Jiang on 12 Nov 2021

Blue
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')

Walter Roberson
Walter Roberson on 12 Nov 2021
You have several options:
  1. You can use the 's' option for input(), and get back a character vector from input(), which you then pass to duration() to parse
  2. 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
  3. 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.
  4. 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()
  5. 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
  6. 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()

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!