• fopen


    转自http://blog.sina.com.cn/s/blog_4b986f1a0101349k.html  

    matlab中fopen函数在指定文件打开的实例如下:

    *1)“fopen”打开文件,赋予文件代号。

    语法1:FID= FOPEN(filename,permission)

    用指定的方式打开文件

    FID=+N(N是正整数):表示文件打开成功,文件代号是N.

    FID=-1 : 表示文件打开不成功。

    FID在此次文件关闭前总是有效的。

    如果以读方式打开,matlab首先搜索工作目录,其次搜索matlab的其他目录,“permission”是打开方式参数。

    打开方式参数由以下字符串确定:

    % 'r' open file for reading
    % 'w' open file for writing; discard existing contents
    % 'a' open or create file for writing; append data to end of file
    % 'r+' open (do not create) file for reading and writing
    % 'w+' open or create file for reading and writing; discard
    % existing contents
    % 'a+' open or create file for reading and writing; append data
    % to end of file
    % 'W' open file for writing without automatic flushing
    % 'A' open file for appending without automatic flushing



    r 读出

    w 写入(文件若不存在,自动创建)

    a 后续写入(文件若不存在,自动创建)

    r+ 读出和写入(文件应已存在)

    w+ 重新刷新写入,(文件若不存在,自动创建)

    a+ 后续写入(文件若不存在,自动创建))

    W 重新写入,但不自动刷新

    A 后续写入,但不自动刷新


    文件的存储格式:文件打开的默认方式是:二进制。以文本方式打开,可以在方式参

    数“permission”中加入“t”文件将,如“rt”,“wt+”

    matlab中fprintf函数的具体使用方法实例如下:

    fprintf函数可以将数据按指定格式写入到文本文件中。其调用格式为:

    数据的格式化输出:fprintf(fid, format, variables)

    按指定的格式将变量的值输出到屏幕或指定文件

    fid为文件句柄,若缺省,则输出到屏幕

    1 for standard output (the screen) or 2 for standard error. If FID is omitted, output goes to the screen.

    format用来指定数据输出时采用的格式

    %d 整数

    %e 实数:科学计算法形式

    %f 实数:小数形式

    %g 由系统自动选取上述两种格式之一

    %s 输出字符串

    fprintf(fid,format,A)
    说明:fid为文件句柄,指定要写入数据的文件,format是用来控制所写数据格式的格式符,与fscanf函数相同,A是用来存放数据的矩阵。
    例6.9 创建一个字符矩阵并存入磁盘,再读出赋值给另一个矩阵。
    >> a='string';
    >> fid=fopen('d:char1.txt','w');
    >> fprintf(fid,'%s',a);
    >> fclose(fid);
    >> fid1=fopen('d:char1.txt','rt');
    >> fid1=fopen('d:char1.txt','rt');
    >> b=fscanf(fid1,'%s')
    b =
    string

    matlab读txt文件

    fid=fopen('fx.txt','r');
    %得到文件号
    [f,count]=fscanf(fid,'%f %f',[12,90]);
    %把文件号1的数据读到f中。其中f是[12 90]的矩阵
    %这里'%f %f'表示读取数据的形势,他是按原始数据型读出
    fclose(fid);
    %关闭文件
    另外有的txt文件还可以用load来打开
    其语句为
    f=load('fx.txt)
  • 相关阅读:
    LeetCode 34. Find First and Last Position of Element in Sorted Array
    LeetCode 875. Koko Eating Bananas
    LeetCode 560. Subarray Sum Equals K
    Vue 3 响应式原理源码解析 All In One
    git rebase 与 git merge 的区别是什么 All In One
    superscript & subscript symbol All In One
    macOS & ifconfig & ipconfig commands All In One
    Chrome UX Report All In One
    TypeScript Utility Types All In One
    TypeScript Type Manipulation All In One
  • 原文地址:https://www.cnblogs.com/xiaoxuesheng993/p/8146255.html
Copyright © 2020-2023  润新知