Data Import and Export  :Low-Level File I/O

the contents of the file:

   16     5     9     4

    2    11     7    14

    3    10     6    15

   13     8    12     1

   55    55    55    55

Example — Overwriting an Existing Text File.  Replace the third line of the file changing.txt from the previous example with [33 33 33 33]:

将第三行换成33 33 33 33

replaceLine = 3;

myformat = '%5d %5d %5d %5d\n';

% Open the file with permission to read and update.

% Use fgetl, which reads a line at a time,

% to place the file position indicator at the third line.

fid = fopen('changing.txt','r+');

for k=1:(replaceLine-1);

    fgetl(fid);

end;

%将指针移至第三行起始处

% call fseek between read and write operations

fseek(fid, 0, 'cof');

% 在当前位置开始写入

% print the new values

fprintf(fid, myformat, [33 33 33 33]);

%将第三行替换

% close the file

fclose(fid);

 

To view the file, use the type function:

type changing.txt

This command returns the new contents of the file:

   16     5     9     4

    2    11     7    14

   33    33    33    33

   13     8    12     1

   55    55    55    55

 

>> help  fseek

 fseek Set file position indicator.

    STATUS = fseek(FID, OFFSET, ORIGIN) repositions the file position

    indicator in the file associated with the given FID.  fseek sets the

    position indicator to the byte with the specified OFFSET relative to

    ORIGIN.

 

    FID is an integer file identifier obtained from FOPEN.

 

    OFFSET values are interpreted as follows:

        >= 0    Move position indicator OFFSET bytes after ORIGIN.

        < 0    Move position indicator OFFSET bytes before ORIGIN.

 

    ORIGIN values are interpreted as follows:

        'bof' or -1   Beginning of file

        'cof' or  0   Current position in file

        'eof' or  1   End of file

 

    STATUS is 0 on success and -1 on failure.  If an error occurs, use

    FERROR to get more information.

 

>> help fgetl

 fgetl Read line from file, discard newline character.

    TLINE = fgetl(FID) returns the next line of a file associated with file

    identifier FID as a MATLAB string. The line terminator is NOT

    included. Use FGETS to get the next line with the line terminator

    INCLUDED. If just an end-of-file is encountered, -1 is returned. 

 

    If an error occurs while reading from the file, fgetl returns an empty

    string. Use FERROR to determine the nature of the error.

 

    MATLAB reads characters using the encoding scheme associated with the

    file. See FOPEN for more information.

 

    fgetl is intended for use with files that contain newline characters.

    Given a file with no newline characters, fgetl may take a long time to

    execute.

 

    Example

        fid=fopen('fgetl.m');

        while 1

            tline = fgetl(fid);

            if ~ischar(tline), break, end

            disp(tline)

        end

        fclose(fid);

 

>> help ischar

 ischar  True for character array (string).

    ischar(S) returns 1 if S is a character array and 0 otherwise.

 

源文档 <http://hi.baidu.com/curbzz/item/e3661599d4b3e48159146139>

Matlab txt内容替换函数 fgetl fseek的更多相关文章

  1. Oracle内置函数:时间函数,转换函数,字符串函数,数值函数,替换函数

    dual单行单列的隐藏表,看不见 但是可以用,经常用来调内置函数.不用新建表 时间函数 sysdate 系统当前时间 add_months 作用:对日期的月份进行加减 写法:add_months(日期 ...

  2. php使用file函数、fseek函数读取大文件效率分析

    php读取大文件可以使用file函数和fseek函数,但是二者之间效率可能存在差异,本文章向大家介绍php file函数与fseek函数实现大文件读取效率对比分析,需要的朋友可以参考一下. 1. 直接 ...

  3. php中替换函数主要用的几个函数strtr(),str_repalce()。

    php中替换函数主要有strtr(),str_repalce()这两个函数,今天介绍下他们的区别和用法, 先来看看这个php字符串替换函数 strtr()的两种用法: strtr(string,fro ...

  4. python字符串内容替换的方法(转载)

    python字符串内容替换的方法 时间:2016-03-10 06:30:46来源:网络 导读:python字符串内容替换的方法,包括单个字符替换,使用re正则匹配进行字符串模式查找与替换的方法.   ...

  5. JavaScript字符串插入、删除、替换函数

    JavaScript字符串插入.删除.替换函数 说明: 以下函数中前两个函数取出查找字符串的前一部分和后一部分,以用于其他函数.注意,调用一次 replaceString(mainStr,search ...

  6. Oracle的字符替换函数translate用法

    参考文档如下:http://www.banping.com/2009/05/18/oracle_function_translate/ Oracle提供了一个字符替换函数translate,不同于re ...

  7. Matlab调用C语言函数

    Matlab调用C语言函数 如果我有一个用C语言写的函数,实现了一个功能,如一个简单的函数:double add(double x, double y) { return x + y ;}现在我想要在 ...

  8. mysql :SQL语句中的替换函数replace

    replace() 用第三个表达式替换第一个字符串表达式中出现的所有第二个给定字符串表达式. 语法 REPLACE ( 'string_expression1' , 'string_expressio ...

  9. sql中保留一位小数的百分比字符串拼接,替换函数,换行符使用

    select  num ,cast(round(convert(float,isnull((a.Sum_Num-d.Sum_Num),0))/convert(float,c.Sum_Store_Num ...

随机推荐

  1. java网络---流

    网络操作很大一部分功能就是输入和输出数据. 简单归纳就是上传和下载文件.文件也是数据的一种载体. java对数据的操作归并为流. 所以对于数据流的操作定义2个基本类. java.io.OutputSt ...

  2. MSLocalDB

    今天用MSLocalDB做测试,发现保存的中文都变成了??,基本可以确定是排序规则的问题,LocalDB建库默认使用Latin规则,需要改为Chinese_PRC_CI_AS,为了修改规则,需要先修改 ...

  3. 定制Asp.NET 5 MVC内建身份验证机制 - 基于自建SQL Server用户/角色数据表的表单身份验证

    背景 在需要进行表单认证的Asp.NET 5 MVC项目被创建后,往往需要根据项目的实际需求做一系列的工作对MVC 5内建的身份验证机制(Asp.NET Identity)进行扩展和定制: Asp.N ...

  4. 使用ExposedObject对Asp.net MVC中匿名类型的JsonResult做单元测试

    返回JsonResult是MVC中的常见返回值类型,而且简单方便的方式是结合匿名类型一起使用. 比如: public ActionResult PreviewEmail() { …… return J ...

  5. System.nanoTime()的使用

    纳秒 ns(nanosecond):纳秒, 时间单位.一秒的10亿分之一,即等于10的负9次方秒.常用作 内存读写速度的单位. 1纳秒=0.000001 毫秒 1纳秒=0.00000 0001秒 ja ...

  6. 在Myeclipse中添加User Library,用户自己的库

    在Myeclipse中添加User Library,用户自己的库 作用:可以将常用的jar包添加到一个固定的库中,避免每一次都要手动导入. 步骤: 1.选择项目,点击Myeclipse的window菜 ...

  7. JSON实战案例--使用JSON进行数据交换实例

    内容: 1.访问javaScript对象的属性 2.访问Javacript对象数组 3.JSON字符串转换成JavaScript对象 4 .Java对象转换成JSON字符串 5 .使用JSON完成级联 ...

  8. FAQ-Ubuntu12.04 15.04禁止移动介质自动播放

    网上有有很多关于Ubuntu10.04关闭移动介质自动播放的方法,包括在文件管理器里面设置或者使用gconf-editor,但是从12.04开始这两种方法都不再好用了,关于移动介质的处理方法被移到了S ...

  9. [转]artDialog

    本文转自:http://aui.github.io/artDialog/ http://aui.github.io/artDialog/doc/index.html artDialog —— 经典的网 ...

  10. hdu-4452-Running Rabbits

    /* Running Rabbits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...