You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python或Matlab将CSV数据转换为Matlab Timetable?含TimeStep用法

解决Matlab readtimetable 中TimeStep参数的使用问题

你在执行TT=readtimetable('EURUSD_big_data.csv')时触发错误:

Expected a datetime or duration variable to use as RowTimes. Check that the data can be converted to datetime or duration, or supply a "TimeStep" or "SampleRate".

你的CSV数据包含date(格式yyyy.MM.dd)、time(格式HH:mm)及行情字段,且时间序列存在非连续间隔(比如示例中17:03到17:06跳过了两个时间点)。以下是两种解决方法:

1. 优先方案:合并日期时间列生成匹配原始数据的时间索引

这种方法完全贴合原始数据的时间记录,不会生成额外空行,步骤如下:

% 先读取CSV为普通表格
T = readtable('EURUSD_big_data.csv');
% 合并date和time字符串,转换为datetime类型
datetime_str = strcat(T.date, ' ', T.time);
T.RowTimes = datetime(datetime_str, 'InputFormat', 'yyyy.MM.dd HH:mm');
% 转换为timetable,移除原有的date和time列
TT = table2timetable(T(:, 3:end));

2. 使用TimeStep参数(仅适用于需要连续时间序列的场景)

如果你的数据理论上是固定1分钟间隔,只是部分记录缺失,需要生成连续时间索引时,可以用TimeStep参数,同时指定起始时间:

% 定义时间序列的起始点
start_time = datetime('2011.01.02 17:01', 'InputFormat', 'yyyy.MM.dd HH:mm');
% 读取时指定TimeStep为1分钟,自动生成连续时间索引
TT = readtimetable('EURUSD_big_data.csv', 'TimeStep', minutes(1), 'StartTime', start_time);

⚠️ 注意:这种方法会自动补全原始数据中缺失的时间点(比如示例中的17:04、17:05),对应行情字段会填充NaN,如果不需要补全空行,优先用第一种方法。

内容的提问来源于stack exchange,提问作者my enlightend god

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 19:02:04