使用MATLAB读取CSV文件? [英] Reading CSV files with MATLAB?
问题描述
我想使用MATLAB读取.csv文件。这是我的代码:
csvread('out2.csv')
pre>
这是out2.csv的样子:
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
在windows上,我可以读取这个完全相同的文件与xlsread函数没有问题。我目前在linux机器上。当我第一次使用xlsread读取文件,我被告知文件不是以公认的格式,所以我切换到使用csvread。然而,使用csvread,我得到以下错误消息:
使用dlmread时出错(行139)
文件之间的不匹配和格式字符串。
从文件读取数字的问题(第1u行,第2u行)==> / csvread(第48行)的错误
m = dlmread(filename,',',r,c)
23:55:12,129.32,129.33 \ n
我认为日期中的'/'会导致问题。在窗口上,第一列被解释为字符串。在Linux上,它似乎被解释为一个数字,所以它试图读取数字,并在反斜杠失败。这是我认为至少是。任何帮助将非常感激。
解决方案
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};
I am trying to read in a .csv file with MATLAB. Here is my code:
csvread('out2.csv')
This is what out2.csv looks like:
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
On windows I am able to read this exact same file with the xlsread function with no problems. I am currently on a linux machine. When I first used xlsread to read the file I was told "File is not in recognized format" so I switched to using csvread. However, using csvread, I get the following error message:
Error using dlmread (line 139) Mismatch between file and format string. Trouble reading number from file (row 1u, field 2u) ==> /09/2013 23:55:12,129.32,129.33\n Error in csvread (line 48) m=dlmread(filename, ',', r, c)
I think the '/' in the date is causing the issue. On windows, the 1st column is interpreted as a string. On linux it seems to be interpreted as a number, so it tries to read the number and fails at the backslash. This is what I think is going on at least. Any help would be really appreciated.
解决方案
csvread
can only read doubles, so it's choking on the date field. Usetextscan
.fid = fopen('out2.csv'); out = textscan(fid,'%s%f%f','delimiter',','); fclose(fid); date = datevec(out{1}); col1 = out{2}; col2 = out{3};
这篇关于使用MATLAB读取CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!