juaner's Blog

千里之行,始于足下。

文件I/O

juaner posted @ Thu, 18 Aug 2011 20:31:05 +0800 in matlab , 1307 readers

1、打开和关闭文件

fid=fopen('filename','permission') ,其中permission是要对文件进行处理的方式。可以是下面的任一字符串:

  • ‘r’:只读文件,
  • ‘w’:只写文件,如果不存在,就生成新文件。
  • ‘a’:增补文件,在文件尾增加数据,如果文件名不存在,就生成新文件。
  • ‘r+’:读写文件,不生成文件。
  • ‘w+’:创建一个新文件或者删除已有文件内容,并可进行读写操作。
  • ’a+‘:读取和增补文件,如果文件名不存在,就生成新文件。

文件可以以二进制的形式或者文本形式打开,默认是前者,在二进制形势下,字符串不会被特殊对待。如果要求以文本形式打开,则在permission字符串后面加‘t’,例如‘rt+’等。

  fid是一个非负整数,称为文件标识,对于文件的任何操作,都是通过这个标识值来传递的。

   关闭文件的命令为:status=fclose(fid);若打开了多个文件想要全部关闭用:status=fclose('all')。

2、读取二进制文件

  • a=fread(fid),fread可以从文件中读取二进制数据,要验证的话用的命令为:disp(char(a'))。例如: fid=fopen('F:\task\dataout.dat','r') ;data=fread(fid);disp(char(data'));不知道为什么不能直接读出数据?

3、写入二进制文件

  • 格式为:count=fwrite(fid,a,precision),a是待写入的矩阵。返回值是成功写入的数据个数。如
  • fid=fopen('hello.txt','w');count=fwrite(fid,magic(5),'int32');status=fclose(fid);---------写的过程;
  • fid=fopen('hello.txt','r');data=fread(fid,25,'int32');status=fclose(fid);---------------------读的过程。

4、读取文本文件

     若已知ASCII文件的格式,要进行更精确的读取,可用fscanf函数从文件中读取格式化的数据,使用语法如下:

    [a,count]=fscanf(fid,format,size)。format的格式有:%s,%d,%f。size可以指定为:n,inf,[m,n]。

   例如:[a,count]=fscanf(fid,'%f',200);成功得到输入。

5、写入文本文件

      语法格式为:count=fprintf(fid,format,y)。里面各项参数的含义同上。常用的格式说明符如下:

  • %e:科学计数形式;
  • %f:固定小数点位置的数据形式;
  • %g:在上述两种格式中自动选取较短的格式。
  • 但是用这种方法我并没有解决我的问题,不知道是为什么。用下面的方法。

6、提取数据直接使用importdata,

      x=importdata('.\dataout.dat'); 用相对路径来表示的。
      [B,ind]=sort(x);
      C=[ind,B];
     dlmwrite('.\datasort.v2.txt',C,'delimiter','\t','newline','pc');

      其中,delimiter是分隔符,用tab分隔,后面的newline用来换行,pc是值。
     另外还有save的使用,这两个的区别讲的比较好的提供一个链接:

    http://www.aiseminar.cn/bbs/viewthread.php?tid=295

   http://hi.baidu.com/ueszx/blog/item/9e6f80d53dc0f8ce50da4b30.html

 

 

Avatar_small
AAA said:
Wed, 11 May 2022 23:11:59 +0800

I dont think Ive caught all the angles of this subject the way youve pointed them out. Youre a true star, a rock star man. Youve got so much to say and know so much about the subject that I think you should just teach a class about it Start a Credit Card Processing Company

 

=======================================

 

 

I think your blog is getting more and more visitors.~,.*- How to Start a Merchant Service Business

Avatar_small
meidir said:
Sat, 03 Dec 2022 21:53:00 +0800

me and my best friend are members of the local town golf club, i am really excited to play golf :: Macbook air

Avatar_small
sofoj46278 said:
Sun, 15 Jan 2023 01:12:33 +0800

The great intreguing web pages retain me coming here again and again. thank you a great deal. 노래방알바

Avatar_small
meidir said:
Wed, 25 Jan 2023 01:01:44 +0800

Youre so cool! I dont suppose Ive read anything similar to this before. So nice to seek out somebody with many original ideas on this subject. realy appreciation for starting this up. this website is one area that is needed on the net, someone after some originality. helpful work for bringing interesting things on the internet! 성기확대수술

Avatar_small
meidir said:
Thu, 16 Feb 2023 17:32:49 +0800

I really like gathering useful info, this post has got me even more info! 유흥알바

Avatar_small
meidir said:
Fri, 17 Feb 2023 22:28:10 +0800

A well written post, I just given this onto a colleague who was doing slightly analysis on that. And he indeed purchased me breakfast because I discovered it for him .. thus let me reword that: Thankx for the treat! however yeah Thnx for spending the time to talk concerning this, I feel strongly concerning it and revel in reading a lot of on this topic. If potential, as you become expertise, would you mind updating your blog with a lot of info? it’s very useful on behalf of me. two thumb up for this blog! Buy Instagram PVA Accounts

Avatar_small
meidir said:
Tue, 21 Feb 2023 01:44:34 +0800

of course, diamond rings would always be the best type of wedding rings that you can give your wife;; YouTube pva accounts


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter