博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Matlab txt内容替换函数 fgetl fseek
阅读量:5956 次
发布时间:2019-06-19

本文共 2629 字,大约阅读时间需要 8 分钟。

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.

 

源文档 <>

转载于:https://www.cnblogs.com/AI-Algorithms/p/3674290.html

你可能感兴趣的文章
Java学习-集合的理解
查看>>
iOS验证码倒计时(GCD实现)
查看>>
iOS中的过滤器和正则表达式(NSPredicate,NSRegularExpression)
查看>>
canvas和svg
查看>>
结对:复利美化版
查看>>
HDU_2689_Sort it
查看>>
urllib模块使用笔记
查看>>
mysql 连接慢的问题(超过了1秒)
查看>>
Linux嵌入式GDB调试环境搭建
查看>>
java分析jvm常用指令
查看>>
【Linux】Linux 在线安装yum
查看>>
oracle 管理操作 (转)
查看>>
DEV 等待窗口
查看>>
VS2017发布微服务到docker
查看>>
lombok
查看>>
Dev-FAT-UAT-PRO
查看>>
Android开发学习总结(五)——Android应用目录结构分析(转)
查看>>
[PHP]PHP rpc框架hprose测试
查看>>
Atom 编辑器系列视频课程
查看>>
C#三种定时器
查看>>