18-6 ŪGiɮ

fread ¨ç¼Æ¥i±qÀɮפ¤Åª¨ú¤G¶i¨î¸ê®Æ¡A¥¦·|±N¨C¤@­Ó¦ì¤¸²Õ¬Ý¦¨¤@­Ó¾ã¼Æ¡A¨Ã±Nµ²ªG¥H¤@¯x°}¶Ç¦^¡C¨Ò¦p¡AÀÉ®× test2.txt ªº¤º®e¦p¤U¡G

>> type test2.txt This is a test!

±ý¥Î fread ¨ç¼ÆŪ¨ú¦¹ÀɮסA¥i¿é¤J¦p¤U¡G

Example 1: 18-ÀÉ®×Ū¼g/fread01.mfid = fopen('test2.txt', 'r'); myData = fread(fid); char(myData') % ÅçÃÒ©ÒŪ¤Jªº¸ê®Æ¬O§_¥¿½T fclose(fid); ans = This is a test!

¨ä¤¤ char ¥i±N myData ªº¾ã¼ÆÂন ASCII ¦r¤¸¡A¨ú myData ªºÂà¸m¥u¬O¬°¤F¨Ï¦L¥Xªº®ÄªG©ö©ó¾\Ū¡C

§Ú­Ì¥i¥Î fread ¨ç¼Æªº²Ä¤G­Ó¿é¤J¤Þ¼Æ¨Ó±±¨î¶Ç¦^¯x°}ªº¤j¤p¡A¨Ò¦p¡G

Example 2: 18-ÀÉ®×Ū¼g/fread02.mfid = fopen('test2.txt', 'r'); myData = fread(fid, 4) % ¥uŪ 4 ­Ó¦ì¤¸²Õ fclose(fid); myData = 84 104 105 115

¦¹®É myData ¬°ªø«×¬° 4 ªº¦V¶q¡C¦P²z¡G

Example 3: 18-ÀÉ®×Ū¼g/fread03.mfid = fopen('test2.txt', 'r'); myData = fread(fid, [2 3]) fclose(fid); myData = 84 105 32 104 115 105

¦¹®É myData ¬° 2X3 ªº¯x°}¡C

¨Ï¥Î fread ªº²Ä¤T­Ó¿é¤J¤Þ¼Æ¡A§Ú­Ì¥i¥H±±¨î fread ±N¤G¶i¨î¸ê®ÆÂন MATLAB ¯x°}©Ò¥Îªººë½T«×¡A¥]§t¡G

±`¥Îªººë·Ç«×¦³¤U¦C´XºØ¡G

Hint
¨ä¥L¸û¤Ö¥Î¨ìªººë·Ç«×¦³¡uint8¡v¤Î¡ufloat32¡vµ¥µ¥¡C¬Û¦Pªº¸ê®Æ¦b¤£¦Pªº¥­¥x¤W¥i¯à¦û¦³¤£¥­ªº¦ì¤¸¼Æ¡A¦Ó¥B¨CºØ¥­¥x©Ò¥Îªººë·Ç¦r¦ê³£²¤¦³¤£¦P¡A±ýª¾¸Ô±¡¡A¥i¿é¤J¡uhelp fread¡v¥H¨ú±o»P¥­¥x¬ÛÃöªº½u¤W¸ê°T¡C

Ĭ

¨Ò¦p¡G­Y¦³¤@ÀÉ®×¥]§tªø¾ã¼Æªº¤G¶i¨î¸ê®Æ¡A«h¤U¦C«ü¥O»yªk¥i¥HŪ¨ú 20 ­Óªø¾ã¼Æ¡A¨Ã¶Ç¦^¤@­Ó¦æ¦V¶q A¡G

A = fread(fid, 20, 'long');

¦b¤U¦C½d¨Ò¤¤¡A§Ú­Ì±N text2.txt ¥Hµu¾ã¼Æªº¤è¦¡Åª¤J¡A´«¥y¸Ü»¡¡A¤]´N¬O¥H 16 bit ¬°¤@­Ó³æ¦ì¨ÓÂà´«¦¨¾ã¼Æ¡A¦]¦¹¡AŪ¥X¨Óªº²Ä¤@­Ó¼Æ¦r¡A·|µ¥©ó±N ¡¥h¡¦ ©M ¡¥T¡¦ Âà´«¦¨ 8 bit¡A¦b¨Ã±Æ¦¨ 16 bits¡AµM«á¦AÂà´«¦¨¤Q¶i¦ìªº¾ã¼Æ­È¡C½d¨Ò¦p¤U¡G

Example 4: 18-ÀÉ®×Ū¼g/fread04.mfid = fopen('test2.txt', 'r'); myData = fread(fid, 1, 'short') fclose(fid); myData2 = bin2dec([dec2bin(abs('h'),8), dec2bin(abs('T'),8)]) myData = 26708 myData2 = 26708

¦b¤W­z½d¨Ò¤¤¡A­n¯S§Oª`·Nªº¬O¡GÁöµM­ìÀɮתº«e¨â­Ó¦ì¤¸²Õ¬O ¡¥T¡¦ ©M ¡¥h¡¦¡A¦ý¬O¦b¥H 16 bits ¬°¤@­Ó³æ¦ì®É¡A¡¥T¡¦ ·|¬O LSB¡]Least Significant Bits¡^ ¦Ó ¡¥h¡¦ «h¬O MSB¡]Most Significant Bits¡^¡A¦]¦¹§Ú­Ì¦A­«²Õ¦¨ 16 bits ®É¡A¡¥h¡¦ ·|¦b«e¦Ó ¡¥T¡¦ ·|¦b«á¡C


MATLABµ{¦¡³]­p¡G¤Jªù½g