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.
Please report any bugs to email@example.com.
If you find this work useful, please consider a small donation:
@Omri Bar-Elli, thanks for your kind words. I just fixed the warning; should not happen anymore.
Great function. Thank you!
On Matlab R2016b it gives a warning:
Warning: NARGCHK will be removed in a future release. Use NARGINCHK or NARGOUTCHK instead.
> In seconds2human (line 46)
Thank you, works nicely.
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.
Love it. Only complaint is the name is too long (had to rename it).
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.
Worked perfectly for me. Thanks.
good... I was nice :)
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?
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 ^_^
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".
[linked to Github]
- Added 'decennium' as a time unit
Updated contact info
- 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.
updated code thanks to Jos' comments.