【小技巧】Matlab读取CSV文件
2016-07-27 14:21
441 查看
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 Yes
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 %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 %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开关
grades.txt内容如下
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('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', ...
'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,' ', 1);
%读剩下的部分
lastpart = textscan(lyric(pos+1:end), '%s');
3、当成数据库使用
具体方法可以去百度“matlab 数据库编程”
注意: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 Yes
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 %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 %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开关
grades.txt内容如下
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('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', ...
'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,' ', 1);
%读剩下的部分
lastpart = textscan(lyric(pos+1:end), '%s');
3、当成数据库使用
具体方法可以去百度“matlab 数据库编程”
相关文章推荐
- mysql load data 导出、导入 csv
- C#导出数据到CSV文件的通用类实例
- C#实现导入CSV文件到Excel工作簿的方法
- php操作csv文件代码实例汇总
- php读取csv文件并输出的方法
- C#操作CSV文件类实例
- php使用指定编码导出mysql数据到csv文件的方法
- 解析在main函数之前调用函数以及对设计的作用详解
- C#中csv文件与DataTable互相导入处理实例解析
- 详解Matlab中 sort 函数用法
- javascript实现客户端兼容各浏览器创建csv并下载的方法
- 解析csv数据导入mysql的方法
- php导出csv数据在浏览器中输出提供下载或保存到文件的示例
- 如何在symfony中导出为CSV文件中的数据
- java和matlab画多边形闭合折线图示例讲解
- Python使用cx_Oracle模块将oracle中数据导出到csv文件的方法
- MySQL 导出数据为csv格式的方法
- mysql使用SQLyog导入csv数据不成功的解决方法
- C#调用Matlab生成的dll方法的详细说明
- MySQL如何导入csv格式数据文件解决方案