QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 1099|回复: 1
打印 上一主题 下一主题

Matlab中你不熟悉的---读写二进制数据

[复制链接]
字体大小: 正常 放大
回帖奖励 1 点体力 回复本帖可获得 1 点体力奖励! 每人限 1 次

413

主题

36

听众

1854

积分

升级  85.4%

  • TA的每日心情
    开心
    2019-9-18 21:55
  • 签到天数: 258 天

    [LV.8]以坛为家I

    社区QQ达人

    群组2015国赛冲刺

    群组2016美赛公益课程

    群组国赛讨论

    群组第三届数模基础实训

    群组Matlab讨论组

    跳转到指定楼层
    1#
    发表于 2015-10-11 18:26 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta
    读写二进制数据
    在本例中显示的脚本文件创建了一个含有10000个随机数的数组,以只写方式打开一个自定义文件,用64位浮点数格式把这个数据写入磁盘,并关闭文件。程序打开所要读取的文件,并读取数组,得到一个100×100的数组。它用来说明二进制I/O操作。
    % Script file: binary_io.m
    %
    % Purpose:
    % To illustrate the use of binary i/o functions.
    %
    % Record of revisions:
    % Date Programmer Description of change
    % ==== ========== =====================
    % 12/19/98 S. J. Chapman Original code
    %
    % Define variables:
    % count                 --Number of values read / written
    % fid                   --File id
    % filename              --File name
    % in_array              --Input array
    % msg                   --Open error message
    % out_array             --Output array
    % status                --Operation status
    % Prompt for file name
    filename = input('Enter file name: ','s');
    % Generate the data array
    out_array = randn(1,10000);
    % Open the output file for writing.
    [fid,msg] = fopen(filename,'w');
    % Was the open successful?
    if fid > 0
        % Write the output data.
        count =fwrite(fid,out_array,'float64');
        % Tell user
        disp([int2str(count) 'values written...']);
        % Close the file
        status = fclose(fid);
    else
        % Output file open failed. Display message.
        disp(msg);
    end
    % Now try to recover the data. Open the
    % file for reading.
    [fid,msg] = fopen(filename,'r');
    % Was the open successful?
    if fid > 0
        % Read the input data.
        [in_array, count] =fread(fid,[100 100],'float64');
        % Tell user
        disp([int2str(count) 'values read...']);
        % Close the file
        status = fclose(fid);
    else
        % Input file open failed.Display message.
        disp(msg);
    end
    当这个程序运行时,结果如下
    >> binary_io
    Enter file name: testfile
    10000 values written...
    10000 values read...
    在当前目录下,有一个80000字节的文件testfile被创建,这个文件之所以占80000个字节,是因为它含有10000个64位的值,每一个值占8个字节。


    zan
    转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
    数学中国版主团队!

    413

    主题

    36

    听众

    1854

    积分

    升级  85.4%

  • TA的每日心情
    开心
    2019-9-18 21:55
  • 签到天数: 258 天

    [LV.8]以坛为家I

    社区QQ达人

    群组2015国赛冲刺

    群组2016美赛公益课程

    群组国赛讨论

    群组第三届数模基础实训

    群组Matlab讨论组

    1. % Script file: binary_io.m
    2. %
    3. % Purpose:
    4. % To illustrate the use of binary i/o functions.
    5. %
    6. % Record of revisions:
    7. % Date Programmer Description of change
    8. % ==== ========== =====================
    9. % 12/19/98 S. J. Chapman Original code
    10. %
    11. % Define variables:
    12. % count                 -- Number of values read / written
    13. % fid                   -- File id
    14. % filename              -- File name
    15. % in_array              -- Input array
    16. % msg                   -- Open error message
    17. % out_array             -- Output array
    18. % status                -- Operation status
    19. % Prompt for file name
    20. filename = input('Enter file name: ','s');
    21. % Generate the data array
    22. out_array = randn(1,10000);
    23. % Open the output file for writing.
    24. [fid,msg] = fopen(filename,'w');
    25. % Was the open successful?
    26. if fid > 0
    27.     % Write the output data.
    28.     count = fwrite(fid,out_array,'float64');
    29.     % Tell user
    30.     disp([int2str(count) ' values written...']);
    31.     % Close the file
    32.     status = fclose(fid);
    33. else
    34.     % Output file open failed. Display message.
    35.     disp(msg);
    36. end
    37. % Now try to recover the data. Open the
    38. % file for reading.
    39. [fid,msg] = fopen(filename,'r');
    40. % Was the open successful?
    41. if fid > 0
    42.     % Read the input data.
    43.     [in_array, count] = fread(fid,[100 100],'float64');
    44.     % Tell user
    45.     disp([int2str(count) ' values read...']);
    46.     % Close the file
    47.     status = fclose(fid);
    48. else
    49.     % Input file open failed. Display message.
    50.     disp(msg);
    51. end
    复制代码
    附上代码
    数学中国版主团队!
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2025-6-2 16:08 , Processed in 0.683429 second(s), 59 queries .

    回顶部