This function converts a (usually large) amount of seconds to human-readable string. For example,
>> str = seconds2human(1463456.3)
'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')
'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.
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.
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?
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".
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