csvread
只能读取双精度数,所以它在日期字段上令人窒息。使用textscan
.
fid = fopen('out2.csv');
out = textscan(fid,'%s%f%f','delimiter',',');
fclose(fid);
date = datevec(out{1});
col1 = out{2};
col2 = out{3};
更新(2017 年 8 月 31 日)
由于这是 2013 年写的,MATLAB 的textscan https://www.mathworks.com/help/matlab/ref/textscan.html#inputarg_formatSpec功能已更新为直接读取日期和时间。现在代码如下所示:
fid = fopen('out2.csv');
out = textscan(fid, '%{MM/dd/uu HH:mm:ss}D%f%f', 'delimiter', ',');
fclose(fid)
[date, col1, col2] = deal(out{:});
下面@Victor Hugo提到的另一种选择(目前我个人对这种情况的选择)是使用readtable https://www.mathworks.com/help/matlab/ref/readtable.html它将接受与以下相同的格式字符串textscan
但将结果直接组装到表对象中:
dataTable = readtable('out2.csv', 'Format', '%{MM/dd/uu HH:mm:ss}D%f%f')
dataTable.Properties.VariableNames = {'date', 'col1', 'col2'};
dataTable =
3×3 table
date col1 col2
___________________ ______ ______
03/09/2013 23:55:12 129.32 129.33
03/09/2013 23:55:52 129.32 129.33
03/09/2013 23:56:02 129.32 129.33