环境: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文件,其内容如下
例1.1
csvread('csvlist.csv')
ans =
例1.2
m = csvread('csvlist.dat', 2, 0)
m =
例1.3
m = csvread('csvlist.dat', 2, 0, [2,0,3,3])
m =
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
Joe
Bill
程序如下
fid = fopen('scan1.dat');
C = textscan(fid, '%s %s �2 � %u %f %f %s');
fclose(fid);
返回值C是一个1×8的元胞数组,其值如下
C{1} = {'Sally'; 'Joe'; 'Bill'}
C{2} = {'Level1'; 'Level2'; 'Level3'}
C{3} = [12.34; 23.54; 34.9]
C{4} = [45; 60; 12]
C{5} = [4294967295; 4294967295; 200000]
C{6} = [Inf; -Inf; 10]
C{7} = [NaN; 0.001; 100]
C{8} = {'Yes'; 'No'; 'No'}
注意:C{5}的前两项超出了uint32数值范围,所以只给uint32的数值上限
例2.3
%去除scan1.dat中地2列的字符串
fid = fopen('scan1.dat');
C = textscan(fid, '%s Level%u8 �2 � %u %f %f %s');
fclose(fid);
返回一个1×8的元胞数组,其中
C{2} = [1; 2; 3]
例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,
7,
程序如下
fid = fopen('data.csv');
C = textscan(fid, '%f %f %f %f %u32 %f', 'delimiter', ',', ...
fclose(fid);
返回一个1×6的元胞数组
C{1} = [1; 7]
C{2} = [2; 8]
C{3} = [3; 9]
C{4} = [4; -Inf]
C{5} = [0; 11]
C{6} = [6; 12]
例2.6
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', ',', ...
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', ',', ...
fclose(fid);
返回一个1×4的元胞数组
C{1} = [1; 5]
C{2} = [2; 6]
C{3} = [3; 7]
C{4} = [4; 8]
例2.8 使用collectoutput开关
grades.txt内容如下
Student_ID
collectoutput开关的默认值为0(false)将CSV中的每一列返回到Cell的一列中。如果将其值设为1(true),则会把相同数据类型的列返回到Cell的一列中。
%默认不开启collectoutput
fid = fopen('grades.txt');
% 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', ...
fclose(fid);
使用collectoutput后,ID成为cell中的一列,Test1和test2合起来成为cell中的一列,test3成为cell中的一列
C_data0 =
[4x1 int32]
C_data1 =
frewind的作用是让后面的textscan函数使用前面的fid,一个fid只能让一个textscan读
例2.9
如果要读的字符串中包含一些控制字符:
b
n
r
t
\
如果你的数据使用不同的控制字符,在调用textscan时能使用sprintf函数显式转换这些控制字符。
lyric = sprintf('Blackbirdfsingingfinfthe
C = textscan(lyric, '%s', 'delimiter', sprintf('f'));
textscan returns a 1-by-1 cell array C:
C{1} =
例2.10
lyric = 'Blackbird singing in the dead of night'
%读取第一个单词:
[firstword, pos] = textscan(lyric,'�', 1);
%读剩下的部分
lastpart = textscan(lyric(pos+1:end), '%s');
3、当成数据库使用
具体方法可以去百度“matlab