`
deepfuture
  • 浏览: 4412318 次
  • 性别: Icon_minigender_1
  • 来自: 湛江
博客专栏
073ec2a9-85b7-3ebf-a3bb-c6361e6c6f64
SQLite源码剖析
浏览量:80131
1591c4b8-62f1-3d3e-9551-25c77465da96
WIN32汇编语言学习应用...
浏览量:70350
F5390db6-59dd-338f-ba18-4e93943ff06a
神奇的perl
浏览量:103593
Dac44363-8a80-3836-99aa-f7b7780fa6e2
lucene等搜索引擎解析...
浏览量:286571
Ec49a563-4109-3c69-9c83-8f6d068ba113
深入lucene3.5源码...
浏览量:15054
9b99bfc2-19c2-3346-9100-7f8879c731ce
VB.NET并行与分布式编...
浏览量:67786
B1db2af3-06b3-35bb-ac08-59ff2d1324b4
silverlight 5...
浏览量:32292
4a56b548-ab3d-35af-a984-e0781d142c23
算法下午茶系列
浏览量:46075
社区版块
存档分类
最新评论

matlab-控制文件读写位置及格式化读写ascii

 
阅读更多

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.
 
    Example:
 
        fseek(fid,0,-1)
 
    "rewinds" the file.

fseek用于定位

 1)读

 

>> fid=fopen('test.m','r')

fid =

     3

>> fseek(fid,5,'bof')

ans =

     0

>> a=fread(fid,5,'uint8=>char')

a =

1
 
0
 
;

>>

 

>> fseek(fid,0,'bof')

ans =

     0

>> a=fread(fid,5,'uint8=>char')

a =

P
=
[
1
 

>>

 

2)写

>> fid=fopen('test.m','r+')

fid =

     3

>> fseek(fid,0,'eof')

ans =

     0

>> fwrite(fid,'%增加一行','char')

ans =

     5

>> fclose(fid)

ans =

     0

>>



 

定位于当前位置写,注意这种写是改写

>> fid=fopen('test.m','r+')

fid =

     3

>> fseek(fid,0,'cof')

ans =

     0

>> ftell(fid)

ans =

     0

>> fwrite(fid,'%增加上面一行','char')

ans =

     7

>> fclose(fid)

ans =

     0

>>

 

ftell得到当前位置

 help ftell
 FTELL Get file position indicator.
    POSITION = FTELL(FID) returns the location of the file position
    indicator in the specified file.  Position is indicated in bytes
    from the beginning of the file.  If -1 is returned, it indicates
    that the query was unsuccessful. Use FERROR to determine the nature
    of the error.
 
    FID is an integer file identifier obtained from FOPEN.

 

 

3、读取行

1)fgetl

>> fid=fopen('test.m','r')

fid =

     4

>> mline=fgetl(fid)

mline =

%增加上面一行 1];

>> mline=fgetl(fid)

mline =

T=[0 1 0];

>> mline=fgetl(fid)

mline =

w=[0 0 ];

>> mline=fgetl(fid)

mline =

[S,Q]=size(T)

>> mline=fgetl(fid)

mline =

b=0;

>>

2)fgets

与fgetl的区别就是,fgets是保留换行符读取一行


>> mline=fgets(fid)

mline =

A=purelin(w*P+b);

 

>>

 

4、格式化读写ascii

1)读

>> a=fscanf(fid,'%s',10)

a =

e=T-A;LP.lr=maxlinlr(P)%误差平方和sse=sumsqr(e);whilesse>0.0000001dW=learnwh([],P,[],[],[],[],e,[],[],[],LP,[]);dB=learnwh(b,ones(1,Q),[],[],[],[],e,[],[],[],LP,[]);

>>

 

>> a=fscanf(fid,'%s')

a =

w=w+dW;b=b+dB;A=purelin(w*P+b)e=T-Asse=sumsqr(e)end%增加一行

>>

 

 help fscanf
 FSCANF Read formatted data from file.
    [A,COUNT] = FSCANF(FID,FORMAT,SIZE) reads data from the file specified
    by file identifier FID, converts it according to the specified FORMAT
    string, and returns it in matrix A. COUNT is an optional output
    argument that returns the number of elements successfully read.
   
    FID is an integer file identifier obtained from FOPEN.
   
    SIZE is optional; it puts a limit on the number of elements that
    can be read from the file; if not specified, the entire file
    is considered; if specified, valid entries are:
 
        N      read at most N elements into a column vector.
        inf    read at most to the end of the file.
        [M,N]  read at most M * N elements filling at least an
               M-by-N matrix, in column order. N can be inf, but not M.
 
    If the matrix A results from using character conversions only and
    SIZE is not of the form [M,N] then a row vector is returned.
 
    FORMAT is a string containing ordinary characters and/or C language
    conversion specifications. Conversion specifications involve the
    character %, optional assignment-suppressing asterisk and width
    field, and conversion characters d, i, o, u, x, e, f, g, s, c, and
    [. . .] (scanset). Complete ANSI C support for these conversion
    characters is provided consistent with 'expected' MATLAB behavior.
    For a complete conversion character specification, see the Language
    Reference Guide or a C manual.
 
    If %s is used an element read may cause several MATLAB matrix
    elements to be used, each holding one character.  Use %c to read
    space characters; the format %s skips all white space.
 
    MATLAB reads characters using the encoding scheme associated with
    the file. See FOPEN for more information. If the format string
    contains ordinary characters, MATLAB matches each of those characters
    with a character read from the file after converting both to the
    MATLAB internal representation of characters.
 
    Mixing character and numeric conversion specifications causes the
    resulting matrix to be numeric and any characters read to show up
    as their numeric values, one character per MATLAB matrix element.
 
    FSCANF differs from its C language namesake in an important respect -
    it is "vectorized" in order to return a matrix argument. The format
    string is recycled through the file until an end-of-file is reached
    or the amount of data specified by SIZE is read in.
 
    Examples:
        S = fscanf(fid,'%s')   reads (and returns) a character string.
        A = fscanf(fid,'%5d')  reads 5-digit decimal integers.

 

 

2)写

>> fid=fopen('test.m','r+')

fid =

     4

>> fseek(fid,0,'eof')

ans =

     0

>> x=[123,3452]

x =

         123        3452

>> fprintf(fid,'%d %6.2f',x)

ans =

    11

>> fclose(fid)

ans =

     0

>>

 

help fprintf
 FPRINTF Write formatted data to text file.
    FPRINTF(FID, FORMAT, A, ...) applies the FORMAT to all elements of
    array A and any additional array arguments in column order, and writes
    the data to a text file.  FID is an integer file identifier.  Obtain
    FID from FOPEN, or set it to 1 (for standard output, the screen) or 2
    (standard error). FPRINTF uses the encoding scheme specified in the
    call to FOPEN.
 
    FPRINTF(FORMAT, A, ...) formats data and displays the results on the
    screen.
 
    COUNT = FPRINTF(...) returns the number of bytes that FPRINTF writes.
 
    FORMAT is a string that describes the format of the output fields, and
    can include combinations of the following:
 
       * Conversion specifications, which include a % character, a
         conversion character (such as d, i, o, u, x, f, e, g, c, or s),
         and optional flags, width, and precision fields.  For more
         details, type "doc fprintf" at the command prompt.
 
       * Literal text to print.
 
       * Escape characacters, including:
             \b     Backspace            ''   Single quotation mark
             \f     Form feed            %%   Percent character
             \n     New line             \\   Backslash
             \r     Carriage return      \xN  Hexadecimal number N
             \t     Horizontal tab       \N   Octal number N
         For most cases, \n is sufficient for a single line break.
         However, if you are creating a file for use with Microsoft
         Notepad, specify a combination of \r\n to move to a new line.
 
    Notes:
 
    If you apply an integer or string conversion to a numeric value that
    contains a fraction, MATLAB overrides the specified conversion, and
    uses %e.
 
    Numeric conversions print only the real component of complex numbers.
 
    Example: Create a text file called exp.txt containing a short table of
    the exponential function.
 
        x = 0:.1:1;
        y = [x; exp(x)];
        fid = fopen('exp.txt','w');
        fprintf(fid,'%6.2f  %12.8f\n',y);
        fclose(fid);
 
    Examine the contents of exp.txt:
 
        type exp.txt
 
    MATLAB returns:
           0.00    1.00000000
           0.10    1.10517092
                ...
           1.00    2.71828183
 

 

 

最后是关闭文件

help fclose
 FCLOSE Close file.
    ST = FCLOSE(FID) closes the file associated with file identifier FID,
    which is an integer value obtained from an earlier call to FOPEN. 
    FCLOSE returns 0 if successful or -1 if not.  If FID does not represent
    an open file, or if it is equal to 0 (standard input), 1 (standard
    output), or 2 (standard error), FCLOSE throws an error.
 
    ST = FCLOSE('all') closes all open files, except 0, 1 and 2.
    See also fopen, fclose, fscanf, fread, fwrite, sprintf, disp.

  • 大小: 23.6 KB
分享到:
评论

相关推荐

    matlab文件读写程序的汇总

    在MATLAB中,文件的读写是编程过程中常见的任务,主要涉及处理文本文件和二进制文件。以下是对MATLAB中几种主要文件读写函数的详细解析: 1. **textread**: `textread`函数是用于读取文本文件的便捷工具。它能...

    Matlab中txt文件的读写操作命令及操作.pdf

    `fread`用于读取二进制文件,而`fscanf`则适用于格式化的文本文件。在例子中,`fscanf(fid, '%f')`会尝试读取文件中的浮点数。 在处理文本文件时,`fscanf`非常灵活,可以按照指定的格式读取数据。例如,`fscanf...

    matlab开发-stlWriteBinaryOrAsciistLFile文件

    在MATLAB编程环境中,STL(STereoLithography)文件是一种常见的3D模型格式,广泛用于3D打印和计算机图形学。`stlWriteBinaryOrAsciiSTLFile`函数是用来将MATLAB中的3D网格数据写入STL文件,支持二进制和ASCII两种...

    matlab中数据文件的处理

    - **`fscanf` 和 `fprintf`**:用于读写文本文件,格式控制严格。 - **`fgetl` 和 `fgets`**:按行读取文本文件,适用于格式未知的情况。 **示例:** ```matlab fid = fopen('example.txt', 'r'); line = fgetl(fid...

    对文件进行输入和输出txt文件格式matlab

    `%f`、`%s`是格式化字符串,分别对应浮点数和字符串类型的数据。`num`、`txt`和`raw`分别存储了读取到的数据。 此外,MATLAB的`csvread`函数适用于CSV格式的TXT文件,它可以自动将数据解析为矩阵: ```matlab data...

    matlab文件IO

    MATLAB还提供了其他文件操作函数,如`fopen/fclose`用于打开和关闭文件,`fread/fwrite`处理二进制数据,`fscanf/fprintf`用于格式化的文本数据读写,`fgetl/fgets`用于读取文件行,以及`ferror/feof/fseek/ftell`...

    pbm/pgm/ppm图片的读写(Matlab)

    Matlab作为强大的数值计算和数据可视化环境,提供了处理这种格式图像的功能。本文将详细探讨如何在Matlab中读取和写入PBM、PGM和PPM图片,并介绍相关的编程知识。 首先,PBM(Portable Bitmap)用于黑白图像,PGM...

    matlab开发-文件和目录导航工具.zip.zip

    - `fprintf`与`fscanf`:格式化写入和读取数据,前者用于将数据按照指定格式写入文件,后者用于按格式从文件读取数据。 - `textscan`与`textwrite`:用于处理非结构化文本数据的读写。 - `load`与`save`:导入和...

    matlab文件读写程序的汇总.pdf

    matlab 文件读写程序的汇总 Matlab 文件读写是 Matlab 编程中非常重要的一部分,包括读取文件和写入文件。下面将对 Matlab 中的四种文件读写方式进行汇总。 1. textread 函数 textread 函数是 Matlab 中最常用的...

    matlab中将数据输出保存为txt格式文件的方法_matlab源码.rar

    在MATLAB中,将数据输出并保存为TXT格式文件是一项常见的任务,这有助于在不同的软件或平台之间交换数据。以下是一些关于如何在MATLAB中实现这一操作的详细步骤和相关知识点。 首先,我们需要理解MATLAB中的数据...

    Matlab的数据文件IO的实现方法

    除了上述高级命令外,Matlab还提供了一系列低级文件I/O函数,如`fopen`、`fclose`、`fprintf`、`fscanf`等,这些函数提供了更多的灵活性,允许用户自定义数据的读写格式。 ##### fopen与fclose - **fopen**: 打开...

    Matlab地各种大数据读取、文件资料读写等操作汇总情况.pdf

    `fprintf`类似于C语言的printf,允许格式化输出,而`save`或`.savetxt`则将工作空间的变量保存为ASCII格式。 3. **二进制数据的处理** - **读取二进制数据**:使用`fread`函数读取二进制数据,它接受文件标识和要...

    Matlab批量导出.coe文件

    - **fprintf**:格式化输出到打开的文件,可以将数据写入到指定的文件中。 - **fclose**:关闭已打开的文件,确保数据完整写入。 3. **导出单个.coe文件**: 以下是一个简单的Matlab脚本示例,用于生成一个包含...

    MATLAB编程.pdf

    - MATLAB的格式化选项:包括short、long、short e、long e、bank等格式化方式。 9. MATLAB矩阵和向量运算: - 矩阵运算与向量运算的区别和使用。 - 矩阵的点运算(element-wise operations)与矩阵运算(matrix ...

    matlab程序设计入门档案读写.pptx

    通过这些基本的文件读写指令,我们可以方便地在 MATLAB 中处理各种类型的数据,这对于数据分析、模型建立和结果可视化等工作至关重要。了解并熟练掌握这些指令,将极大地提升 MATLAB 编程的效率和实用性。

    INIFILE:INI 文件读写。-matlab开发

    综上所述,MATLAB 的 `inifile` 功能为处理 ASCII 格式的 INI 文件提供了便利,其高效、灵活的特点使其成为管理配置数据的理想选择。开发者可以通过学习和熟练掌握 `inifile`,提高代码的可维护性和用户体验。

    stlread.rar_STLRead.m_matlab_matlab stl_stlread_stl格式 matlab

    STL(STereo Lithography)格式是3D打印和计算机辅助设计(CAD)领域中广泛使用的文件格式,它主要用于存储三维几何数据。MATLAB是一款强大的数值计算和数据分析软件,但默认并不支持直接读取STL文件。为了在MATLAB...

    matlab写文件.pdf

    默认加载的也是MATLAB二进制格式的文件,加载的文件名可以省略,MATLAB会尝试加载与当前脚本同名的`.mat`文件。 4. **文件存取管理** - `fopen`函数:用于打开文件,返回一个文件标识符(FID)。基本语法有: - `...

    matlab开发-plywrite

    这个格式是便携式的,支持ASCII和二进制两种模式,具有较好的读写效率。 PLY文件格式由斯坦福大学开发,最初用于他们的“Stanford 3D Scanning Repository”。这种格式包含了顶点、面和其他属性(如颜色、纹理坐标...

    matlab开发-surf2stl

    需要注意的是,STL文件可以是二进制或ASCII格式,二进制格式更紧凑,读写速度更快,但不如ASCII格式易于阅读。 在MATLAB中,使用surf2stl函数可能如下: ```matlab % 假设已有的表面数据 [x, y] = meshgrid...

Global site tag (gtag.js) - Google Analytics