Need to convert string and character to time in Matlab

I have a string that reads "12-38-09" and a 1x6 char that is 123804. I need both of these to be displayed as a HH:MM:SS format of time. How can I do that? Also, I am not including dates, just times.

 Accepted Answer

Unfortunately, the 'inputformat' option for duration() is much less flexible than for datetime()
duration(sscanf('12-38-09', '%2d-%2d-%2d').')
duration(sscanf('123809', '%2d%2d%2d').')
If you have a bunch of these, you might alter the code a bit depending on how they are stored.

3 Comments

Thank you! Is there a way to make it more general instead of using exact numbers? I'm trying to write a code and my variable is eyeTrackingTime. Is there a way to insert a variable to get the duration time since running it multiple times there will always be a different number for the time?
duration(sscanf(eyeTrackingTime, '%2d-%2d-%2d').')
duration(sscanf(eyeTrackingTime, '%2d%2d%2d').')
If you do not know ahead of time whether the variable will have the dashes or not, then we could deal with that too.
duration(str2double(regexp(eyeTrackingTime,'\d\d', 'match')))
You might need .' after the )) before the third )
Thank you so much that worked!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!