MATLAB社区

MATLAB、社区和更多

在MATLAB中使用日期

三个星期前我写了一篇关于MATLAB的新电子表格导入工具。之后我有一些谈话关于在MATLAB中使用日期;日期是常见的列标题或表数据。工具将Excel日期导入MATLABdatenum年代。在MATLAB datenum只是一个双代表任何日期&时间1月1日午夜公元0000年。MATLAB附带几个有用的功能来处理这些特殊情况的数字,特别是显示它们。

现在,时钟,日期功能将提供当前日期和时间datenum,datevec,或detestr,分别。datenum时一个数字代表的日期和时间,datevec将一年,月,日,小时,分钟和第二个组件1 x6向量。datestr字符串表示的日期时间。有很多格式化选项可供如何显示日期和时间(见下文)。

currentTimeAndDate =现在currentTimeAndDateAsVector =时钟currentDateAsString =日期
currentTimeAndDate = 7.3484 e + 05 currentTimeAndDateAsVector = 2011 11 11月28日8 50 20.486 currentDateAsString = 28 - - 2011

就数据而言,这三个日期类型使用转换函数是可以互换的。您可以使用datenum,datevec,datestr函数的三种类型之间的转换。单个函数执行计算约会通常喜欢一个特定的格式,例如addtodate寻找datenum类型。

currentDateAsNumber = datenum (currentDateAsString) currentDateAndDtimeAsNumber = datenum (currentTimeAndDate)%注意上面的区别currentDate = datestr (currentTimeAndDate) currentDateAsVector = datevec (currentDateAsString)
currentDateAsNumber = 734835 currentDateAndDtimeAsNumber = 7.3484 e + 05 currentDate = 28日- 11月- 2011 08:50:20 currentDateAsVector = 2011 11 28日0 0 0

使用的日期我特别感兴趣的是阴谋。假设有一些时变数据和我们希望x轴反映这些日期。如果我只是阴谋datenum数据,默认情况下,x-labels都将大,丑陋的数字。

firstInMonths = repmat((2011年1 1 0 0 0),12日1);firstInMonths (:, 2) = 1:12;栏(datenum (firstInMonths),兰德(12)* 10)
随机的日期数据

值得庆幸的是,MATLAB附带了一个简单的函数,datetick将这些数字转化为字符串:

datetick
与蜱虫随机日期数据

不幸的是,这些标记标签不是很漂亮。让我们使用的日期格式datetick函数来指定如何我们希望标签。对于这个函数,我们可以使用任何承认的日期格式detestr函数(见下文)。

datetick (“x”,“mmm-yy”)
随机日期数据与格式良好的蜱虫

最后,给你参考,这是帮助信息datestr甲酸,列出了如何一个日期字符串:

帮助datestr
DATESTR字符串表示的日期。S = DATESTR向量V (V)将一个或多个日期日期字符串S V输入必须是M-by-6向量矩阵包含M全(six-element)日期。每个元素必须积极的双精度数V。DATESTR返回一个列向量的日期字符串,其中M日期向量的总数在v . S = DATESTR (N)将一个或多个串行数字N日期字符串S N输入参数可以是一个标量,矢量,或积极的双精度数的多维数组。DATESTR返回一个列向量的日期字符串,其中M是日期数字的总数n S = DATESTR (D, F)将一个或多个日期向量,串行日期数字或日期字符串D到相同数量的日期字符串S输入参数F是一个数字或字符串格式决定日期的格式字符串输出。有效值为F表1中给出,如下。输入F也可能包含一个自由格式的日期格式的字符串组成的令牌格式如表2所示,如下。日期字符串2-character年解释是在100年内围绕。S = DATESTR (S1、F P)转换日期字符串S1日期字符串,应用F格式输出字符串,并使用主年P的起始年100年高价票的一年所在范围。默认的主是当前每年- 50年。 F = -1 uses the default format. S = DATESTR(...,'local') returns the string in a localized format. The default (which can be called with 'en_US') is US English. This argument must come last in the argument sequence. Note: The vectorized calling syntax can offer significant performance improvement for large arrays. Table 1: Standard MATLAB Date format definitions Number String Example =========================================================================== 0 'dd-mmm-yyyy HH:MM:SS' 01-Mar-2000 15:45:17 1 'dd-mmm-yyyy' 01-Mar-2000 2 'mm/dd/yy' 03/01/00 3 'mmm' Mar 4 'm' M 5 'mm' 03 6 'mm/dd' 03/01 7 'dd' 01 8 'ddd' Wed 9 'd' W 10 'yyyy' 2000 11 'yy' 00 12 'mmmyy' Mar00 13 'HH:MM:SS' 15:45:17 14 'HH:MM:SS PM' 3:45:17 PM 15 'HH:MM' 15:45 16 'HH:MM PM' 3:45 PM 17 'QQ-YY' Q1-96 18 'QQ' Q1 19 'dd/mm' 01/03 20 'dd/mm/yy' 01/03/00 21 'mmm.dd,yyyy HH:MM:SS' Mar.01,2000 15:45:17 22 'mmm.dd,yyyy' Mar.01,2000 23 'mm/dd/yyyy' 03/01/2000 24 'dd/mm/yyyy' 01/03/2000 25 'yy/mm/dd' 00/03/01 26 'yyyy/mm/dd' 2000/03/01 27 'QQ-YYYY' Q1-1996 28 'mmmyyyy' Mar2000 29 (ISO 8601) 'yyyy-mm-dd' 2000-03-01 30 (ISO 8601) 'yyyymmddTHHMMSS' 20000301T154517 31 'yyyy-mm-dd HH:MM:SS' 2000-03-01 15:45:17 Table 2: Free-form date format symbols Symbol Interpretation of format symbol =========================================================================== yyyy full year, e.g. 1990, 2000, 2002 yy partial year, e.g. 90, 00, 02 mmmm full name of the month, according to the calendar locale, e.g. "March", "April" in the UK and USA English locales. mmm first three letters of the month, according to the calendar locale, e.g. "Mar", "Apr" in the UK and USA English locales. mm numeric month of year, padded with leading zeros, e.g. ../03/.. or ../12/.. m capitalized first letter of the month, according to the calendar locale; for backwards compatibility. dddd full name of the weekday, according to the calendar locale, e.g. "Monday", "Tuesday", for the UK and USA calendar locales. ddd first three letters of the weekday, according to the calendar locale, e.g. "Mon", "Tue", for the UK and USA calendar locales. dd numeric day of the month, padded with leading zeros, e.g. 05/../.. or 20/../.. d capitalized first letter of the weekday; for backwards compatibility HH hour of the day, according to the time format. In case the time format AM | PM is set, HH does not pad with leading zeros. In case AM | PM is not set, display the hour of the day, padded with leading zeros. e.g 10:20 PM, which is equivalent to 22:20; 9:00 AM, which is equivalent to 09:00. MM minutes of the hour, padded with leading zeros, e.g. 10:15, 10:05, 10:05 AM. SS second of the minute, padded with leading zeros, e.g. 10:15:30, 10:05:30, 10:05:30 AM. FFF milliseconds field, padded with leading zeros, e.g. 10:15:30.015. PM set the time format as time of morning or time of afternoon. AM or PM is appended to the date string, as appropriate. Examples: DATESTR(now) returns '24-Jan-2003 11:58:15' for that particular date, on an US English locale DATESTR(now,2) returns 01/24/03, the same as for DATESTR(now,'mm/dd/yy') DATESTR(now,'dd.mm.yyyy') returns 24.01.2003 To convert a non-standard date form into a standard MATLAB dateform, first convert the non-standard date form to a date number, using DATENUM, for example, DATESTR(DATENUM('24.01.2003','dd.mm.yyyy'),2) returns 01/24/03. See also DATE, DATENUM, DATEVEC, DATETICK. Reference page in Help browser doc datestr

如果你有兴趣使用MATLAB的日期,这里是一个链接到日期的功能这是可用的。

|
  • 打印

评论

留下你的评论,请点击在这里MathWorks账户登录或创建一个新的。