4.0

4.0 | 4 ratings Rate this file 7 downloads (last 30 days) File Size: 2.17 KB File ID: #22977

Convert an amount of seconds to more readable units

by Rody Oldenhuis

 

14 Feb 2009 (Updated 18 Feb 2009)

Code covered by BSD License  

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

Download Now | Watch this File

File Information
Description

This file converts any given amount of seconds to more readable quantities. For example,

n = seconds2human(1e6)

returns the string

' 1 week, 4 days, 13 hours, 46 minutes, 40 seconds. '

Note that also the string 'months' can be returned. One month is defined in this program as 30.471 days, so the results might be a bit inaccurate in these cases.

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
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".

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)

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

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.

31 Mar 2009 Shahab Anbarjafari

good... I was nice :)

12 Apr 2009 Andrew

Worked perfectly for me. Thanks.

Please login to add a comment or rating.
Updates
18 Feb 2009

updated code thanks to Jos' comments.

Tag Activity for this File
Tag Applied By Date/Time
hours Cristina McIntire 17 Feb 2009 14:07:53
week Cristina McIntire 17 Feb 2009 14:07:53
seconds Cristina McIntire 17 Feb 2009 14:07:53
days Cristina McIntire 17 Feb 2009 14:07:53
convenience Rody Oldenhuis 17 Feb 2009 14:08:03
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com