【问题标题】:Importing Excel Data with Dates and Values and Plotting Time Series in Matlab在 Matlab 中导入带有日期和值的 Excel 数据并绘制时间序列
【发布时间】:2015-06-30 06:13:51
【问题描述】:

我想在 Excel 中的数据集的 Matlab 中绘制时间序列。

Excel 文件如下所示:

Data:       |  Value:

2005-04-01  |  5.20

2006-12-02  |  3.12

...

如何将它加载到 Matlab 中并绘制它的时间序列?

【问题讨论】:

  • 提示:看看xlsreadplot
  • @am304 是的......但我当然已经这样做了 :-) 然后我将日期作为字符串获取,这是我不想要的......
  • 那么你应该在你的问题中告诉我们你尝试了什么,你的代码,你卡在哪里,以及任何错误消息。目前,您的问题过于模糊,表明您尚未尝试任何方法。
  • 我认为这个问题很清楚,尽管还没有尝试过任何东西。
  • @p_thomson 你能验证my solution 是否是你要找的吗?

标签: matlab datetime time-series import-from-excel


【解决方案1】:

有两种简单的绘制日期的方法,但我会给你先从 xls 文件中读取的脚本。

% Read from Excel
[N,T] = xlsread( filepath );

然后您需要按如下方式提取/转换日期。日期是文本的第一列。

d = datetime( T(:,1) );

然后你可以如下绘制变量

figure;
plot( d, N(:,1) );

这里有一个示例图

或者,如果您希望使用以下行将日期作为整数而不是 datetime 对象,则可以使用 datenum 而不是 datetime

d = datenum( T(:,1) );

【讨论】:

    【解决方案2】:

    使用 xlsread 将数据作为字符串加载,现在可以通过多种方式转换日期和值,日期最严格的方式可能是使用 str2num,它允许您读取第 N 个字符作为一个数字,例如:

    string = "2005-04-01|5.20"
    year = str2num(string(1:4))
    month = str2num(string(6:7)) //%where the number is the N th character in string, and has to be numerical or the str2num will return error
    
    //%Note that this method does not read non-numerical strings, in your case "-" "|" will not be read. 
    

    我相信这是更快的方法之一,因为它主要涉及对标准数据格式的类型转换。还有其他建议吗?

    PS:在您的时间序列图中,我建议将您的日期转换为长数字,即 20050401、20061202 等;这将是最有效的。 (你可以按年*1000+月*100+天做到这一点。

    【讨论】:

    • 虽然此解决方案将有助于解析/提取年份和月份,但我认为这不是 OP 想要的
    • 另外,长数字不是线性缩放,这是您通常想要的
    • 我的datenum 方法会更好,但这已经包含在我的解决方案中
    猜你喜欢
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 2018-11-13
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    相关资源
    最近更新 更多