Highlights from
Convert seconds to human readable string(s)

4.375

4.4 | 9 ratings Rate this file 15 Downloads (last 30 days) File Size: 2.7 KB File ID: #22977

Convert seconds to human readable string(s)

by

 

14 Feb 2009 (Updated )

Convert an amount of seconds to hours/days/weeks/etc.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

This function converts a (usually large) amount of seconds to human-readable string. For example,
    >> str = seconds2human(1463456.3)
    str =
        'About 2 weeks and 2 days.'
You may also call the function with a second input argument; either 'short' (the default) or 'full'. This determines the level of detail returned in the string:

    >> str = seconds2human(1463456.3, 'full')
    str =
        '2 weeks, 2 days, 22 hours, 30 minutes, 56 seconds.'

The 'short' format returns only the two largest units of time.

Furthermore, [secs] may be an NxM-matrix, in which case the output is an NxM cell array of the corresponding strings.

Please report any bugs to oldenhuis@gmail.com.

If you find this work useful, please consider a donation:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6G3S5UYM7HJ3N

MATLAB release MATLAB 7.9 (R2009b)
Other requirements None that I know of.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (11)
08 Mar 2014 Naor Movshovitz  
05 Mar 2014 J.R.! Menzinger  
13 Jul 2011 Esteban Echeverria

Amazing. I had to change the format a little to get my answer in time format. 00:00:00. Still great :) Very good for waitbars.

28 Feb 2011 Husam Aldahiyat

Love it. Only complaint is the name is too long (had to rename it).

22 Dec 2009 Jesse Blocher

Works quite well - only critique is that it doesn't handle <0.5 seconds very well. It just returns an empty string. Perhaps is could default to "< 1 second" for anything less than that.

12 Apr 2009 Andrew

Worked perfectly for me. Thanks.

31 Mar 2009 Gholamreza (Ĺ ahab) Anbarjafari

good... I was nice :)

18 Feb 2009 Rody Oldenhuis

yup, they are, depending though on your definition of "scientific". They are not SI units (the only unit of time there is the second), but based on properly defined quantities (second, year) and thus derived units, used in many standards such as ISO-8601 and JD in astronomy.

Anyway, thanks fer the comments, much appreciated ^_^ I didn't know ETIME yet, thanks for that too.

18 Feb 2009 Jos (10584)

The code has indeed improved and comments have been added, as well as a readable help section (missing, however, a reference to, e.g., DATENUM and ETIME, which also may be used to get the elapsed time in a more readable format).

Still, I doubt it's usefulness and emphasis on details when unneeded. I think 3 stars is a nice rating for this snippet.

btw, are weeks, months, or even days proper scientific units?

18 Feb 2009 Rody Oldenhuis

woops! I actually did not check this -ancient- piece of code before I uploaded it. My bad, this program came from a time when I was learning the basics of matlab. Anyway, should be lots better now.

Coincidentally, "Eon" is not a scientific unit of time, so I did not include it ^_^

18 Feb 2009 Jos (10584)

You missed the eons!

More importantly, you should provide a proper help section, giving details of the input and expected output.The code is also not that nice. Internal comments are almost lacking. MatLab already offers DEBLANK and STRTRIM for trimming white spaces. Moreover, this function is not needed if you had used cell arrays to store the names. The section of "switch plural(amount), case true, ... case false ..., end is much simpler codes as: if amount>1, ... else ... end.

Finally, is it really useful to know the number of seconds remaining when something took millenia, or even days? This focus on unimportant details reminds me of an old russian joke: A man has saved enough money to buy a car in the old Sovjet-Union. The car-dealer tells him that he can pick up his new car two years from now. The man asks: "In the morning or in the afternoon? ". "Why do you care? It is 2 years from now" the car-dealer replies. "Well", the man says, "the plumber is coming in the morning".

Updates
18 Feb 2009

updated code thanks to Jos' comments.

11 Feb 2010

- reduced the default verbosity, as suggested by Jos. The verbosity can now be changed with a second argument.

- Included (sec < 1) case, as suggested by Jesse.

14 Feb 2014

Updated contact info

28 Feb 2014

- Added 'decennium' as a time unit
- Implemented more robust error-checking
- Small documentation update

17 Jun 2014

[linked to Github]

Contact us