操作命令:
>>[pathstr,name,ext,versn]
= fileparts(file)
%获取文件路径的各种信息
例如:
[pathstr,name,ext,versn] =
fileparts('D:\winxp-old\课件-教程\课件\matlab教学\matlab宝典源代码\Ch13\13.4.2\Compli_ball.m')
pathstr =
D:\winxp-old\课件-教程\课件\matlab教学\matlab宝典源代码\Ch13\13.4.2
name =
Compli_ball
ext =
.m
versn =
''
>>[fid,message] =
fopen('file','permission')
%打开文件
'r' Open file for reading (default).读文件
'w' Open file, or create new file, for writing; discard existing
contents, if any.
打开文件,创建新文件;将原有的内容去掉。
'a' Open file, or create new file, for writing; append data to
the end of the file.
打开文件,创建新文件;原有的内容之后添加数据。
'r+' Open file for reading and writing.读写文件
'w+' Open file, or create new file, for reading and writing;
discard existing contents, if any.
读写文件,创建新文件;将原有的内容去掉。
'a+' Open file, or create new file, for reading and writing;
append data to the end of the file.打开文件,创建新文件;原有的内容之后添加数据。
'A' Append without automatic flushing; used with tape
drives.非自动冲洗添加
'W' Write without automatic flushing; used with tape
drives.非自动冲洗写。
删除文件
>>status =
fclose(fid);
%删除文件,必须要关闭句柄
>>delete
file;
读取文件
>>data =
fread(fid);
%得到数值数组
>>disp(char(data'))
%显示字符串
写入二进制文件
>>fwrite(fid,inputdata,mode)
%fid是文件句柄 inputdata输入的数据
处理文本文件
>>M =
csvread(filename, row, col, range)
%Read comma-separated value file 读取逗号分割的文件
Specify range using the notation [R1 C1 R2 C2] where (R1,C1) is
the upper left corner of the data to be read and (R2,C2) is the
lower right corner.
>>M =
dlmread(filename, delimiter)
%可以设置数据分隔符
>>fgetl
%Read line from file, discarding newline
character每次读一行,去掉新行符
>>fgets
%Read line from file, keeping newline
character每次读一行,不去掉新行符