csvwrite
将数据写入文件,数据间以逗号分隔
dlmread
将以 ASCII 码分隔的数值数据读入到矩阵中
dlmwrite
将矩阵数据写入到文件中,以 ASCII 分隔
textread
从文本文件中读入数据,将结果分别保存
textscan
从文本文件中读入数据,将结果保存为单元数组
下面详细介绍这些函数。
1. csvread、csvwrite
csvread 函数的调用格式如下:
● M = csvread('!lename'),将文件 !lename 中的数据读入,并且保存为
M,!lename 中只能包含数字,并且数字之间以逗号分隔。M 是一个数组,行
数与 !lename 的行数相同,列数为 !lename 列的最大值,对于元素不足的行,
以 0 补充。
● M = csvread('!lename', row, col),读取文件 !lename 中的数据,起始
行为 row,起始列为 col,需要注意的是,此时的行列从 0 开始。
● M = csvread('!lename', row, col, range),读取文件 !lename 中的数
据,起始行为 row,起始列为 col,读取的数据由数组 range 指定,range 的
格 式 为 : [R1 C1 R2 C2] , 其 中 R1 、 C1 为 读 取 区 域 左 上 角 的 行 和 列 ,
R2、C2 为读取区域右下角的行和列。
csvwrite 函数的调用格式如下:
● csvwrite('!lename',M),将数组 M 中的数据保存为文件 !lename,数据
间以逗号分隔。
● csvwrite('!lename',M,row,col),将数组 M 中的指定数据保存在文件中,
数据由参数 row 和 col 指定,保存 row 和 col 右下角的数据。
● csvwrite 写入数据时每一行以换行符结束。另外,该函数不返回任何值。
这两个函数的应用见下面的例子。
例 13-4 函数 csvread 和 csvwrite 的应用。
本例首先将 MATLAB 的图标转化为灰度图,将数据存储在文本文件中,再
将其部分读出,显示为图形。
编写 M 文件,命名为 immatlab.m,内容为:
% the example of functions csvread and csvwrite
I_MATLAB= imread('D:\matlab.bmp'); % read in the image
I_MATLAB= rgb2gray(I_matlab); % convert the image to gray
image
!gure,imshow(I_matlab,'InitialMagni!cation',100); % show the
image
csvwrite('D:\matlab.txt',I_matlab); % write the data into a text
!le
sub_MATLAB= csvread('D:\matlab.txt',100,100);% read in part