Matlab 从入门到精通 Chapter11 文件读取I/O

时间:2023-03-09 14:50:24
Matlab 从入门到精通 Chapter11 文件读取I/O

11.1 工作空间数据读取

将工作空间的变量保存为文件,可以使用save命令。

  •  save('filename') 将文件保存在当前目录下,文件名为filename.mat
  • save('filename','var1','var2',...)将工作空间内的指定变量保存在filename.mat中
  • save('filename',‘-struct','s') 将标量机构体的所有域保存到文件中
  • save(....,'format') 将文件保存为指定的文件格式,包括MAT,ASCII等格式。
>>a=12381

>> b=[2 34 212 4 3];
>> whos
Name Size Bytes Class Attributes a 1x1 8 double
b 1x5 40 double >> save('var_test.mat','a','b');

  

Matlab 从入门到精通 Chapter11 文件读取I/O

>>save('mydata','-regexp','^x')

>>s1.a=12.7;s1.b={'peking univ',[4 5;6 7]}; s1.c='good work';

>>save newstruct.mat -struct s1

>>dir

>>%查看文件

>>whos -file newstruct.mat

  

通过dir函数可以查看保存在当前目录下的文件

通过whos命令方式查看保存在文件中的数据

从一个指定的文件中读取变量,可以使用load命令

  • load 加载matlab.mat中的所有变量
  • load filename 加载指定文件filename中的所有变量
  • load -mat filename 加载MAT格式的文件filename
  • S=load('arg1','arg2','arg3',...)使用MATLAB的函数格式来调用文件
>> whos -file var_test.mat
Name Size Bytes Class Attributes a 1x1 8 double
b 1x5 40 double >> load var_test
>> whos
Name Size Bytes Class Attributes a 1x1 8 double
b 1x5 40 double >> %保存文件
>> a=magic(4);b=ones(2,4)*-5.7;c=[8 6 4 2];
>> save >> load 正在从 matlab.mat 中加载 >> whos
Name Size Bytes Class Attributes a 4x4 128 double
b 2x4 64 double
c 1x4 32 double

  

11.3 底层文件读取I/O

函数类别

函数

说明

打开或关闭文件

Fopen

打开文件fid=fopen(filename,mode)

fclose

关闭文件status=fclose(fid)

二进制文件读写

fread

读入二进制文件A=read(fid,count,precision)

fwrite

写为二进制文件

[count errmsg]=fwrite(fid,format,size)

格式化读写I/O

fscanf

写为二进制文件

[A,count]=fscanf(fid,format,size)

fprintf

将格式化数据写入到文件中

count=fprintf(fid,format,A,...)

fgetl

从文件中读取行,删除换行符号

[tline,count,msg]=fgetl(obj)

fgets

从文件中读取行,保留换行符号

[tline,count,msg]=fgetl(obj)

字符串转换

sprintf

将格式化数据写入字符串

[s,errmsg]=sprintf(format,A,...)

sscanf

在格式控制下读取字符串

A=sscanf(s,format,size)

文件定位

ferror

查询文件读取的状态信息

Message=ferror(fid)

feof

检测文件读取是否到文件末尾

Eofstat=feof(fid)

fseek

设定文件定位指针

Status=fseek(fid,offset,origin)

ftell

获取文件定位的位置

Position=ftell(fid)

frewind

将文件指针定位到文件开头

Frewind(fid)

文件读取示例:

>> x=0:.1:1;
>> y=[x;exp(x)];
>> fid=fopen('exp.txt','wt');
>> fprintf(fid,'%6.2f%12.8f\n',y);
>> fclose(fid) >> fid=fopen('exp.txt','r');
>> a=fscanf(fid,"%g %g',[2 inf])

 11.4 文件名称处理

fileparts函数可以实现对文件名称各部分的读取和分隔:

[pathstr,name,ext,versn]=fileparts('filename')

返回文件路径的各部分信息,如文件路径pathstr,文件名称name,文件扩展名,文件版本号(versn)

%创建完整的文件路径

f1=fullfile(matlabroot,'toolbox/matlab/general/Contents.m')

11.5 处理二进制文件

读取M文件:通过命令fread进行读取

%读取文件

[fid,message]=fopen('peaks.m','r+')

data=fread(fid)

读取文本文件

  • A=fread(fid)
  • A=fread(fid,count) 读入count个数组元素
  • A=fread(fid,count,precision) 读入count个数组元素,指定精度

count的可选项

  • n:n个元素
  • inf:读入整个文件中的内容
  • [m,n]:读入足够的数据填充m*n的矩阵
>>fid=fopen('dsd.txt','r')

>>c=fread(fid,5)'

>>fclose(fid)

  

写入文本文件

使用fwrite函数:

[count,errmsg]=fwrite(fid,A,precision):将矩阵A的数据写入到指定的文件中

[count,errmsg]=fwrite(fid,A,precision,skip):将矩阵A的数据写入到指定的文件中,skip跳过

>> fid=fopen('bins.txt','wb')

>> fwrite(fid,magic(5),'int32')

>> fclose(fid)

>> fid=fopen('bins.txt','r');
>> data=fread(fid,[5,5],'int32')

  

data =

17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9