将mm/dd/yyyy转换为可用于datenum MATLAB的形式 [英] Convert mm/dd/yyyy to a form usable for datenum MATLAB

查看:264
本文介绍了将mm/dd/yyyy转换为可用于datenum MATLAB的形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将单元格中的一列日期从mm/dd/yyyy格式(它们以4/10/2007的形式,或者以两位数的月份,10/10/2007的形式)转换为yyyy-mm -dd,并绘制它.因此,我需要将mm/dd/yyyy转换为序列号.

I want to convert a column of dates from a cell from the format mm/dd/yyyy (They come in the form 4/10/2007, or with double digit months, 10/10/2007) to yyyy-mm-dd, and plot it. So I need to turn mm/dd/yyyy into serial date number.

当日期列的格式已经为yyyy-mm-dd时,以下代码将起作用:

When the date column is already in the form yyyy-mm-dd, the following code works:

DateString = PM25data(:,11); % Pull out dates
    formatIn = 'yyyy-mm-dd';
    x1 = datenum(DateString,formatIn); % Convert to datnum

但是,由于此处的日期格式为mm/dd/yyyy(例如2007年4月12日),所以我无法使用上述格式处理错误

However, since the date form here is mm/dd/yyyy (for example, 4/12/2007), I can't get the above format to work with the error

DATENUM failed.

Caused by:
    Error using dtstr2dtnummx
    Failed on converting date string to
    date number.

我也尝试了以下代码:

DateString = PM25data(2:end,1);
    formatOut = 'yyyy-mm-dd';
    x4 = datenum(DateString, formatOut); % Convert to datnum

但是它运行此错误:

Cannot convert input into specified date
string.
DATENUM failed.

在这种情况下,如何使datenum正常工作?

How can I get datenum to work in this case?

推荐答案

格式'mm/dd/yyyy'只是datenum中的格式23.所以:

Format 'mm/dd/yyyy' is just format 23 in datenum. So:

>> string = '4/12/2007';
>> datenum(string,23)
ans =
      733144

或明确定义格式:

>> string = '4/12/2007';
>> datenum(string,'mm/dd/yyyy')
ans =
      733144

这篇关于将mm/dd/yyyy转换为可用于datenum MATLAB的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆