使用日期和值导入 Excel 数据并在 Matlab 中绘制时间序列
Importing Excel Data with Dates and Values and Plotting Time Series in Matlab
我想在 Matlab 中绘制我在 Excel 中的数据集的时间序列。
Excel 文件如下所示:
Data: | Value:
2005-04-01 | 5.20
2006-12-02 | 3.12
...
如何将其加载到 Matlab 中并绘制其时间序列?
使用 xlsread 将数据作为字符串加载,现在可以通过多种方式转换日期和值,日期最简单的方法可能是使用 str2num,它允许您读取 N th 字符作为数字,例如:
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+天来计算。
有 2 种绘制日期的简单方法,但我会给你脚本,让你先从 xls 文件中读取。
% Read from Excel
[N,T] = xlsread( filepath );
然后您需要 extract/convert 日期如下。日期是文本的第一列。
d = datetime( T(:,1) );
然后你可以绘制变量如下
figure;
plot( d, N(:,1) );
示例图在这里
或者,如果您希望日期为整数而不是 datetime
对象,则可以使用 datenum
而不是 datetime
使用以下行。
d = datenum( T(:,1) );
我想在 Matlab 中绘制我在 Excel 中的数据集的时间序列。
Excel 文件如下所示:
Data: | Value:
2005-04-01 | 5.20
2006-12-02 | 3.12
...
如何将其加载到 Matlab 中并绘制其时间序列?
使用 xlsread 将数据作为字符串加载,现在可以通过多种方式转换日期和值,日期最简单的方法可能是使用 str2num,它允许您读取 N th 字符作为数字,例如:
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+天来计算。
有 2 种绘制日期的简单方法,但我会给你脚本,让你先从 xls 文件中读取。
% Read from Excel
[N,T] = xlsread( filepath );
然后您需要 extract/convert 日期如下。日期是文本的第一列。
d = datetime( T(:,1) );
然后你可以绘制变量如下
figure;
plot( d, N(:,1) );
示例图在这里
或者,如果您希望日期为整数而不是 datetime
对象,则可以使用 datenum
而不是 datetime
使用以下行。
d = datenum( T(:,1) );