MATALB 读写文本函数csvwrite,csvread,dlmwrite,dlmread,textread,textscan
来源:互联网 发布:淘宝1元秒杀是真的吗 编辑:程序博客网 时间:2024/05/24 02:26
为处理文本文件,MATLAB提供了多种处理函数,常见的函数包括csvread,dlmread和testread等。
1. csvwrite函数
csvwrite函数写入数据时每一行以换行符结束,函数不反悔任何值。csvwrite函数的调用格式如下:
- csvwrite(‘filename’,M):将数组M 中的数据保存为文件filename,数据间以逗号分隔
- csvwrite(‘filename’,M,row,col):将数组 M 中的指定数据保存在文件中,数据由参数
row 和 col 指定,保存 row 和 col 右下角的数据
clear allclcm=[1,2,3;4,5,6;,7,8,9];csvwrite('test.dat',m);type test.dat;
命令行窗口的输出结果为:
1,2,34,5,67,8,9
将数组 m 写入 csvlist.dat 文件中,并在数据链前添加两个数据列
clear allclcm=[1,2,3;4,5,6;,7,8,9];csvwrite('test.dat',m,0,2);type test.dat;
命令行窗口的输出结果为:
,,1,2,3,,4,5,6,,7,8,9
将数组 m 写入 csvlist.dat 文件中,并在数据链前添加 2个数据列和在数据列上方添加两个数据行
clear allclcm=[1,2,3;4,5,6;,7,8,9];csvwrite('test.dat',m,2,2);type test.dat;
命令行窗口的输出结果为:
,,,,,,,,,,1,2,3,,4,5,6,,7,8,9
2. csvread函数
csvread函数的调用格式如下:
- M = csvread(‘filename’):将文件 filename 中的数据读入,并且保存为 M。filename中只能包含数字,并且数字之间以逗号分隔。M 是一个数组,行数与 filename 的行数相同,列数为 filename 列的最大值;对于元素不足的行,以 0 补充。
- M = csvread(‘filename’, row, col):读取文件 filename 中的数据,起始行为 row,起始列为 col。需要注意的是,此时的行、列从 0 开始。
- M = csvread(‘filename’, row, col, range):读取文件 filename 中的数据,起始行为 row,起始列为col,读取的数据由数组 range 指定,range 的格式为[R1 C1 R2 C2],其中 R1、C1 为读取区域左上角的行和列,R2、C2 为读取区域右下角的行和列
A=csvread('test.dat')B=csvread('test.dat',1,1)C=csvread('test.dat',2,2,[2,2,3,3])
命令行窗口的输出结果为:
A = 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 0 0 4 5 6 0 0 7 8 9B = 0 0 0 0 0 1 2 3 0 4 5 6 0 7 8 9C = 1 2 4 5
3. dlmwrite函数
dlmwrite函数用于向文档中写入数据,dlmwrite函数的调用格式如下:
- dlmwrite(‘filename’, M):将矩阵 M 的数据写入文件 filename 中,以逗号分隔
- dlmwrite(filename, M, ‘D’):将矩阵 M 的数据写入文件 filename 中,采用指定的分隔符分隔数据,如果需要 Tab 键,可以用“\t”指定
- dlmwrite(‘filename’, M, ‘D’, R, C):指定写入数据的起始位置
- dlmwrite(filename, M, ‘attrib1’, value1, ‘attrib2’, value2, …):指定任意数目的参数,可以指定的参数如下表
- dlmwrite(‘filename’, M,’-append’):如果 filename 指定的文件存在,则在文件后面写入数据,不指定时则覆盖源文件
- dlmwrite(‘filename’, M,’-append’, attribute-value list):续写文件,并指定参数
应用示例:
m=rand(4)dlmwrite('test1.txt',m,'delimiter','\t','precision',5)type test1.txtdlmwrite('test2.txt',m,'delimiter','\t','precision',2)type test2.txt
结果为:
0.81472 0.63236 0.95751 0.957170.90579 0.09754 0.96489 0.485380.12699 0.2785 0.15761 0.800280.91338 0.54688 0.97059 0.141890.81 0.63 0.96 0.960.91 0.098 0.96 0.490.13 0.28 0.16 0.80.91 0.55 0.97 0.14
向文件中写入多行数据
M=magic(3)dlmwrite('test3.txt',[M*4 M/4],' ')type test3.txtdlmwrite('test3.txt',rand(3),'-append','roffset',1,'delimiter',' ')
结果:
%第一次输出结果32 4 24 2 0.25 1.512 20 28 0.75 1.25 1.7516 36 8 1 2.25 0.5%第二次输出结果32 4 24 2 0.25 1.512 20 28 0.75 1.25 1.7516 36 8 1 2.25 0.50.42176 0.95949 0.849130.91574 0.65574 0.933990.79221 0.035712 0.67874
4. dlmread函数
dlmread函数用于从文档中读入数据,调用格式为:
- M = dlmread(filename)
- M = dlmread(filename, delimiter)
- M = dlmread(filename, delimiter, R, C)
- M = dlmread(filename, delimiter, range)
其中,参数 delimiter 用于指定文件中的分隔符;其他参数的意义与 csvread 函数中参数的意义相同,这里不再赘述。dlmread 函数与 csvread 函数的差别在于,dlmread 函数在读入数据时可以指定分隔符,不指定时默认分隔符为逗号。
5. testread函数
当文件的格式已知时,可以利用textread和textscan函数读入。
- [A,B,C,…] = textread(filename,format)
- [A,B,C,…] = textread(filename,format,N)
其中,format 可以是%d、%f、%s 等。
读取test4.txt的内容,其内容为:test no4 12.12 34 true
[string,no,x,y,flag]=textread('test4.txt',...'%s %s %f %d %s',1)
结果为:
string = 'test'no = 'no4'x = 12.1200y = 34flag = 'true'
读取test4.txt的内容,但no仅显示数字:
[string,no,x,y,flag]=textread('test4.txt',...'%s no%s %f %2d %s',1)
结果为:
string = 'test'no = '4'x = 12.1200y = 34flag = 'true'
阅读全文
0 0
- MATALB 读写文本函数csvwrite,csvread,dlmwrite,dlmread,textread,textscan
- matlab 读文件csvread textread用法实例
- 将csv文件在MATLAB中导入为向量的csvread函数以及将向量导出到csv文件中的csvwrite函数
- Matlab读取文件的一些小trick(textscan与 csvread)
- dlmwrite
- dlmwrite
- Matlab textread函数详解
- Matlab textread函数详解
- Matlab textread函数详解
- Matlab textread函数详解
- Matlab textread函数详解
- matlab中的textread函数
- Matlab textread函数详解
- matlab中textread 函数
- Matlab textread 函数详解
- Matlab textread函数详解
- Matalb归一化函数
- MATALB: sparse函数
- 转载pca的数学原理(看别人转载的格式不太好看,就自己转一下,经典保留)
- 十一、select2实现下拉菜单
- 使用java 8 Optional 的正确姿势
- Flume安装及配置介绍
- SpringBoot GET POST 简单搭建
- MATALB 读写文本函数csvwrite,csvread,dlmwrite,dlmread,textread,textscan
- Linux下 源码 PHP7+Apache2.4整合,安装禅道9.5项目管理软件
- Android 解决底部弹出PopWindow时如果有虚拟按键遮挡问题
- UE4 代理事件(C++)
- hashcode()方法
- Linux功耗管理(26)Linux power supply class(1)_软件架构及API汇整
- LintCode:M-丢失的第一个正整数
- input文本框不可编辑的3种方法
- 2017 ACM/ICPC 广西邀请赛||HDU 6185 Covering 【状压DP+矩阵快速幂】