0

如何显示从 csv 文件中提取的10/9/2013 17:00时间戳以显示在 matlab 图形 x 轴中?

4

1 回答 1

1

让我们将一些带时间戳的数据写入文件

fid = fopen('myfile.csv', 'w');               // open file for writing
for t = 1:10
  fprintf(fid, '%s,%f\n', datestr(now()), t); // write a line
  pause(rand);                                // pause for a bit
end
fclose(fid);                                  // always close your files!

文件内容现在是

11-Oct-2013 09:03:55,1.00000
11-Oct-2013 09:03:56,2.000000
11-Oct-2013 09:03:56,3.000000
11-Oct-2013 09:03:57,4.000000
11-Oct-2013 09:03:57,5.000000
11-Oct-2013 09:03:58,6.000000
11-Oct-2013 09:03:59,7.000000
11-Oct-2013 09:03:59,8.000000
11-Oct-2013 09:04:00,9.000000
11-Oct-2013 09:04:00,10.000000

要使用时间戳读出它,您可以执行

fid = fopen('myfile.csv');                       // open for reading
cts = textscan(fid, '%s %f', 'Delimiter', ',');  // read comma delimited file
d = datenum(cts{1});                             // convert first col to datenum
v = cts{2};                                      
fclose(fid);                                     // always close your files!

并用这样的时间戳标签绘制它

plot(d, v), datetick('x')
于 2013-10-11T08:09:07.977 回答