在csv文件中显示Matlab的时间

时间:2013-10-11 07:09:05

标签: matlab date time graph

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

1 个答案:

答案 0 :(得分: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')