您的位置:首页 > 编程语言 > MATLAB

matlab读写文件的几个常用函数

2017-05-08 20:23 513 查看
1  isempty(filename)  : 判断文件名是否为空

2  [name, path] = uigetfile('*.dat')  :  弹出窗口选择.dat文件,并将名字赋给name, 把路径赋给path

3  f = fullfile(path,name);   将路径与名字组合,输出全称,即路径+文件

4  ischar(filename); 判断文件名是否为字符数组

5  error('   information  ':  弹出红色的错误信息

6  warning('   information ');   弹出橙色的警告信息

7 dir(f): 生成一个文件的结构体

  该结构体包含5 fields:name,  date,  bytes,  isdir, datenum

8 fopen(filename, permission, machineformat)中machineformat有哪些?

  machineformat:

  ‘cray’ or 'c' : Cray floating point with big-endian byte ordering(用大字节存储顺序的克雷浮点)

   ‘ieee-be’ or 'b':  IEEE floating point with big-endian byte ordering(用大字节存储顺序的电气与电子工程师协会浮点

   ‘ieee-le’ or 'l' :   IEEE floating point with little-endian byte ordering(用小字节存储顺序的电气与电子工程师协会浮点

   ‘ieee-be.l64’ or 's':   IEEE floating point with big-endian byte ordering and 64-bit long datetype

   ‘ieee-le.l64’ or 'a':   IEEE floating point with little-endian byte ordering and 64-bit long datetype

   'native' or 'n': numeric format of the machine on which matlab is running

   'vaxd' or 'd' : VAX D floating point and VAX ordering

   'vaxg' or 'g' :  VAX G floating point and VAX

   补充一点:

    IEEE(Institute of Electrical and Electronics Engineers) :  电气与电子工程师协会

   VAX( Virtual Address Extender): 虚拟地址扩展, VAX computer  VAX机

9 setstr():  将ascii码的字符转换为字符串

10 findstr(s1,s2):  在较长的字符串中查找较短字符串出现的次数,并返回其位置。s1,s2的位置在前在后没有关系。

eg: s = 'I begin to get up when it comes to.

      findstr(s,'to')  输出:9   33

11 str2num():   将字符串转化为数值

12   uigetfile(): 让用户选择一个文件来写,其基于图形用户界面。

      调用格式: [fname,pname] = uigetfile('*.dat','Sample Dialog Box')    fname:文件名, pname: 路径名

      uiputfile():基本与uigetfile用法类似。

13 uiimport/importdata(): uiimport用于读取复杂的数据文件,importdata与uiimport功能相似,但不打开GUI。

      eg: D = importdata('sample.txt');

    Name      English      Chinese      Mathmatics

     Wang          99                98                     100

       Li               98                89                      70

     Zhang         80                90                      97

     text = D.textdata(4X4)  % 显示文本内容,cell型

    data = D.data(4X4)  % 显示数据内容,double型

14  textread: 主要用于读取ASCII格式的文本和数值数据文件

   调用格式: [A,B,C,...] = textread(filename,format,N,param,value);

   其中:A、B、C用于存放读取数据的向量,filename为待操作的文件,format用来控制读取的数据格式,N代表重复使用该格式的次数,

   param与value是与特殊操作有关,例如跳过标题行(headerlines)。

   [Name, E,C,M] = textread('sample.txt','%s %d %d %d',4,'headerlines',1);

  strread除从字符串读取外,在读取文件时,类似于textread.

15 load(): 用于读取MAT文件或者用空格间隔的格式相似的ASCII文件;

     save: 将MATLAB变量写入MAT格式文件或者间隔的ASCII文件

    调用格式: save filename [list of variables]  [options]

   options: -mat, -ascii,

   load('filename');  一般用于加载矩阵数据

16 fgetl与fgets: 读取一行数据,当做字符串处理

    调用格式: line = fgetl(fid);

                         line = fgets(fid,nchar);

   fgetl: 读入的字符串中不包括换行符,若读到文件末尾,则返回-1.

   fgets: 读入数据时,保留原文件中的换行符,输入nchar是整型数,如果指定,则读入一行时,最多读nchar个字符

   eg:  

         fid = fopen('  ');

         while 1

                  line = fgets(fid);

                  if line == -1

                       break;

                  end

                  disp(line);

            end

          fclose(fid)

       该程序把文件一行一行地读入到line中,每读一行显示一次,直到文件末尾。

    eg2: 

    数据文件:

     NX10

      100. 100. 100. 100. 100. 100. 100. 100. 100. 100. 100.

     fid = fopen(' ');

     ctmp = fgets(fid);

     ltmp = length(ctmp);

     NX = str2num(ctmp(3:ltmp)); %  数据个数

     DX = fscanf(fid,'%f ', NX)

                  

     
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: