博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
matlab中fprintf, fscanf, fwrite, fread函数的用法
阅读量:4143 次
发布时间:2019-05-25

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

clearclccd('C:\Documents and Settings\Administrator\桌面\matlab\test');a = 1 : 10;fid = fopen('myData.txt', 'w');fprintf(fid, '%d ', a);fclose(fid);

      上述程序生成了myData.txt文件,其中的内容为:

1 2 3 4 5 6 7 8 9 10

 

 

clearclccd('C:\Documents and Settings\Administrator\桌面\matlab\test');fid = fopen('myData.txt', 'r');[a count] = fscanf(fid, '%d', inf);fclose(fid);

      上述程序从myData.txt中读取数据,放在列向量a中,最终count = 10; a = [1;2;3;4;5;6;7;8;9;10];

 

 

clearclccd('C:\Documents and Settings\Administrator\桌面\matlab\test');a = 0 : 255;fid = fopen('myFile.yuv', 'wb');fwrite(fid, a, 'uchar');fclose(fid);

      上述程序是以二进制方式存的0-255, 用文本文件方式打开肯定是乱码, 所以要用UltraEdit打开,真正看到的是存储在里面的比特位, 用UltraEdit看到的结果是: 00 01 02 ...... FE FF

 

 

clearclccd('C:\Documents and Settings\Administrator\桌面\matlab\test');fid = fopen('myFile.yuv', 'rb');[a count] = fread(fid, inf, 'uchar');fclose(fid);

      上述程序读取myFile.yuv文件中的内容,count = 256; a = [0;1;2;...;254;255];

 

 

 

      以上用的都是向量,实际上可以用矩阵。

      有时候,用矩阵更方便。

      为了简便起见,仅仅考虑把数据读到矩阵中(不考虑把矩阵写进文件)

clearclccd('C:\Documents and Settings\Administrator\桌面\matlab\test');fid = fopen('myData.txt', 'r');[a count1] = fscanf(fid, '%d', [5 2]);fclose(fid);fid = fopen('myFile.yuv', 'rb');[b count2] = fread(fid, [16 16], 'uchar');fclose(fid);

 

      count1 = 10; count2 = 256; a和b都是矩阵的形式

a =

     1     6

     2     7
     3     8
     4     9
     5    10

b =

     0    16    32    48    64    80    96   112   128   144   160   176   192   208   224   240

     1    17    33    49    65    81    97   113   129   145   161   177   193   209   225   241
     2    18    34    50    66    82    98   114   130   146   162   178   194   210   226   242
     3    19    35    51    67    83    99   115   131   147   163   179   195   211   227   243
     4    20    36    52    68    84   100   116   132   148   164   180   196   212   228   244
     5    21    37    53    69    85   101   117   133   149   165   181   197   213   229   245
     6    22    38    54    70    86   102   118   134   150   166   182   198   214   230   246
     7    23    39    55    71    87   103   119   135   151   167   183   199   215   231   247
     8    24    40    56    72    88   104   120   136   152   168   184   200   216   232   248
     9    25    41    57    73    89   105   121   137   153   169   185   201   217   233   249
    10    26    42    58    74    90   106   122   138   154   170   186   202   218   234   250
    11    27    43    59    75    91   107   123   139   155   171   187   203   219   235   251
    12    28    44    60    76    92   108   124   140   156   172   188   204   220   236   252
    13    29    45    61    77    93   109   125   141   157   173   189   205   221   237   253
    14    30    46    62    78    94   110   126   142   158   174   190   206   222   238   254
    15    31    47    63    79    95   111   127   143   159   175   191   207   223   239   255

 

 

转载地址:http://wnzti.baihongyu.com/

你可能感兴趣的文章
Pentaho 开发: 在eclipse中构建Pentaho BI Server工程
查看>>
JSP的内置对象及方法
查看>>
android中SharedPreferences的简单例子
查看>>
android中使用TextView来显示某个网址的内容,使用<ScrollView>来生成下拉列表框
查看>>
andorid里关于wifi的分析
查看>>
Spring MVC和Struts2的比较
查看>>
Hibernate和IBatis对比
查看>>
Spring MVC 教程,快速入门,深入分析
查看>>
Android 的source (需安装 git repo)
查看>>
Commit our mod to our own repo server
查看>>
LOCAL_PRELINK_MODULE和prelink-linux-arm.map
查看>>
Simple Guide to use the gdb tool in Android environment
查看>>
Netconsole to capture the log
查看>>
Build GingerBread on 32 bit machine.
查看>>
How to make SD Card world wide writable
查看>>
Detecting Memory Leaks in Kernel
查看>>
Linux initial RAM disk (initrd) overview
查看>>
Timestamping Linux kernel printk output in dmesg for fun and profit
查看>>
There's Much More than Intel/AMD Inside
查看>>
apache和tomcat整合
查看>>