csvwrite
将数据写入文件,数据间以逗号分隔
dlmread
将以 ASCII 码分隔的数值数据读入到矩阵中
dlmwrite
将矩阵数据写入到文件中,以 ASCII 分隔
textread
从文本文件中读入数据,将结果分别保存
textscan
从文本文件中读入数据,将结果保存为单元数组
下面详细介绍这些函数。
1. csvread、csvwrite
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 为
读取区域右下角的行和列。
csvwrite 函数的调用格式如下:
● csvwrite('filename',M),将数组 M 中的数据保存为文件 filename,数据间
以逗号分隔。
● csvwrite('filename',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
figure,imshow(I_matlab,'InitialMagnification',100); % show the
image
csvwrite('D:\matlab.txt',I_matlab); % write the data into a text file
sub_MATLAB= csvread('D:\matlab.txt',100,100);% read in part of the
data
sub_MATLAB= uint8(sub_matlab); % convert the data to uint8
评论0