Matlab读取CSV文件
环境:Matlab R2009a,Win 7
1、用csvread函数
注意:csvread函数只试用与用逗号分隔的纯数字文件
第一种:M = CSVREAD('FILENAME') ,直接读取csv文件的数据,并返回给M
第二种:M = CSVREAD('FILENAME',R,C) ,读取csv文件中从第R-1行,第C-1列的数据开始的数据,这对带有头文件说明的csv文件(如示波器等采集的文件)的读取是很重要的。
第三种:M = CSVREAD('FILENAME',R,C,RNG),其中 RNG = [R1 C1 R2 C2],读取左上角为索引为(R1,C1) ,右下角索引为(R2,C2)的矩阵中的数据。
注意:matlab认为CSV第1行第1列的单元格坐标为(0,0)
给定一个csvlist.csv文件,其内容如下
02, 04, 06, 08, 10, 12
03, 06, 09, 12, 15, 18
05, 10, 15, 20, 25, 30
07, 14, 21, 28, 35, 42
11, 22, 33, 44, 55, 66
例1.1读取整个文件
csvread('csvlist.csv')
ans =
2    4    6    8    10    12
3    6    9    12    15    18
5    10    15    20    25    30
7    14    21    28    35    42
11    22    33    44    55    66
例1.2读取第2行以下,第0列以右区域的数据
m = csvread('csvlist.dat', 2, 0)
m =
5    10    15    20    25    30
7    14    21    28    35    42
11    22    33    44    55    66
例1.3读取第2行以下,第0列以右,第3行以上,第3列以左区域的数据
m = csvread('csvlist.dat', 2, 0, [2,0,3,3])
m =
5    10    15    20
7    14    21    28
2、使用textscan函数
在使用textscan函数前必须用fopen函数打开CSV文件。textscan函数读取的结果会存在cell数组中。
调用格式
C = textscan(fid, 'format')
C = textscan(fid, 'format', N)
C = textscan(fid, 'format', param, value, ...)
C = textscan(fid, 'format', N, param, value, ...)
C = textscan(str, ...)
[C, position] = textscan(...)
关于textscan函数的具体用法见help textscan。
例2.1读取字符串
str = '0.41 8.24 3.57 6.24 9.27';
C = textscan(str, '%3.1f %*1d');
textscan returns a 1-by-1 cell array C:
C{1} = [0.4; 8.2; 3.5; 6.2; 9.2]
例2.2读取不同类型的数据
scan1.dat文件内容如下
Sally  Level1  12.34  45  1.23e10  inf  NaN  Yesmatlab 下载
Joe    Level2  23.54  60  9e19    -inf  0.001 No
Bill  Level3  34.90  12  2e5      10    100  No
程序如下
fid = fopen('scan1.dat');
C = textscan(fid, '%s %s %f32 %d8 %u %f %f %s');
fclose(fid);
返回值C是一个1×8的元胞数组,其值如下
C{1} = {'Sally'; 'Joe'; 'Bill'}          class cell
C{2} = {'Level1'; 'Level2'; 'Level3'}    class cell
C{3} = [12.34; 23.54; 34.9]              class single
C{4} = [45; 60; 12]                      class int8
C{5} = [4294967295; 4294967295; 200000]  class uint32
C{6} = [Inf; -Inf; 10]                  class double
C{7} = [NaN; 0.001; 100]                class double
C{8} = {'Yes'; 'No'; 'No'}              class cell
注意:C{5}的前两项超出了uint32数值范围,所以只给uint32的数值上限
例2.3去除一列字符串
%去除scan1.dat中地2列的字符串
fid = fopen('scan1.dat');
C = textscan(fid, '%s Level%u8 %f32 %d8 %u %f %f %s');
fclose(fid);
返回一个1×8的元胞数组,其中
C{2} = [1; 2; 3]                        class uint8
例2.4只读第一列
fid = fopen('scan1.dat');
names = textscan(fid, '%s %*[^\n]');
fclose(fid);
返回一个1×1的元胞数组
names{1} = {'Sally'; 'Joe'; 'Bill'}
例子2.5指定的分隔符和空值的换算
data.csv文件内容如下
1,  2,  3,  4,  ,  6
7,  8,  9,  , 11, 12
程序如下
fid = fopen('data.csv');
C = textscan(fid, '%f %f %f %f %u32 %f', 'delimiter', ',', ...            'EmptyValue', -Inf);
fclose(fid);
返回一个1×6的元胞数组
C{1} = [1; 7]          class double
C{2} = [2; 8]          class double
C{3} = [3; 9]          class double
C{4} = [4; -Inf]        class double (empty converted to -Inf) C{5} = [0; 11]          class uint32 (empty converted to 0)
C{6} = [6; 12]          class double
例2.6  CSV文件中含有空值和注释
data2.csv内容如下
abc, 2, NA, 3, 4
// Comment Here
def, na, 5, 6, 7
分离出注释语句
fid = fopen('data2.csv');
C = textscan(fid, '%s %n %n %n %n', 'delimiter', ',', ...
'treatAsEmpty', {'NA', 'na'}, ...
'commentStyle', '//');
fclose(fid);
返回1×5的元胞数组
C{1} = {'abc'; 'def'}
C{2} = [2; NaN]
C{3} = [NaN; 5]
C{4} = [3; 6]
C{5} = [4; 7]
例2.7处理重复分隔符
data3.csv 内容如下
1,2,3,,4
5,6,7,,8
将multipledelimsasone参数的值赋为1,剔除重复的分隔符
fid = fopen('data3.csv');
C = textscan(fid, '%f %f %f %f', 'delimiter', ',', ...
'MultipleDelimsAsOne', 1);
fclose(fid);
返回一个1×4的元胞数组
C{1} = [1; 5]
C{2} = [2; 6]
C{3} = [3; 7]
C{4} = [4; 8]
例2.8使用collectoutput开关
<内容如下
Student_ID  | Test1  | Test2  | Test3
1          91.5    89.
2    A
2          88.0    67.8    B
3          76.3    78.1    C
4          96.4    81.2    D
collectoutput开关的默认值为0(false)将CSV中的每一列返回到Cell的一列中。如果将其值设为1(true),则会把相同数据类型的列返回到Cell的一列中。
%默认不开启collectoutput
fid = fopen('');
% read column headers
C_text = textscan(fid, '%s', 4, 'delimiter', '|');
% read numeric data
C_data0 = textscan(fid, '%d %f %f %s')
%开启collectoutput
frewind(fid);
C_text = textscan(fid, '%s', 4, 'delimiter', '|');
C_data1 = textscan(fid, '%d %f %f %s', ...
'CollectOutput', 1)
fclose(fid);
使用collectoutput后,ID成为cell中的一列,Test1和test2合起来成为cell中的一列,test3成为cell中的一列
C_data0 =
[4x1 int32]    [4x1 double]    [4x1 double]    {4x1 cell}
C_data1 =
[4x1 int32]    [4x2 double]    {4x1 cell}
frewind的作用是让后面的textscan函数使用前面的fid,一个fid只能让一个textscan 读
例2.9使用缺省的控制字符
如果要读的字符串中包含一些控制字符:
\b Backspace
\n Newline
\r Carriage return
\t Tab
\\Backslash (\)
如果你的数据使用不同的控制字符,在调用textscan时能使用sprintf函数显式转换这些控制字符。
lyric = sprintf('Blackbird\fsinging\fin\fthe\fdead\fof\fnight');
C = textscan(lyric, '%s', 'delimiter', sprintf('\f'));
textscan returns a 1-by-1 cell array C:
C{1} =
{'Blackbird'; 'singing'; 'in'; 'the'; 'dead'; 'of'; 'night'}
例2.10读取部分字符串
lyric = 'Blackbird singing in the dead of night'
%读取第一个单词:
[firstword, pos] = textscan(lyric,'%9c', 1);
%读剩下的部分
lastpart = textscan(lyric(pos+1:end), '%s');
3、当成数据库使用
具体方法可以去百度“matlab  数据库编程”