18-4 �g�J ASCII �ɮ�

fprintf ¨ç¼Æ¥i±N¸ê®Æ¨Ì®æ¦¡«ü©w¦r¦ê¨Ó¼g¤J ASCII ÀɮסA¨ä¨Ï¥Î»yªk¦p¤U¡G

fprintf(fid, format, y)

¨ä¤¤ fid ¬O±ý¼g¤J¤§Àɮתº¿ëÃѽX¡]¥Ñ fopen ²£¥Í¡^¡Aformat ¬O®æ¦¡«ü©w¦r¦ê¡A¥Î¥H«ü©w¸ê®Æ¼g¦ÜÀɮתº®æ¦¡¡Ay «h¬O MATLAB ªº¸ê®ÆÅܼơC±`¥Îªº®æ¦¡«ü©w¦r¦ê¦³¤U¦C´XºØ¡G

¨ä¥L¦UºØ®æ¦¡«ü©w¦r¦ê¥i¿é¤J help fprintf ¨Ó±o¨ì¸Ô²Óªº½u¤W»¡©ú¡C¨Ò¦p¡A±ý±N¤@¥­¤è®Úªí¼g¤J squareRootTable.txt¡A¥i¿é¤J¦p¤U¡G

Example 1: 18-ÀÉ®×Ū¼g/fprintf01.mx = 1:10; y = [x; sqrt(x)]; fid = fopen('squareRootTable.txt', 'w'); fprintf(fid, 'Table of square root:\r\n'); fprintf(fid, '%2.0f => %10.6f\r\n', y); fclose(fid); dos('start squareRootTable.txt'); % ¶}±Ò squareRootTable.txt

¦b¤W¨Ò¤¤¡A%2.0f ¦L¥XªºÁ`Äæ¼e¬° 2¡A¥B¤£±a¤p¼Æ¡A¦Ó %10.6f ¦L¥XªºÁ`Äæ¼e¬° 10¡A¥]§tÄæ¼e¬° 6 ªº¤p¼Æ³¡¥÷¡C¡]ŪªÌ¥i¥Ñ¶}±Òªº squareRootTable.txt ¨ÓÅçÃÒ¤W­zªº¦C¦L®æ¦¡¡C¡^

sprintf ¨ç¼Æ©M fprintf ¨ç¼Æªº¥\¯à«ÜÃþ¦ü¡A°ß¤@¤£¦Pªº¬O¡Asprintf ±N¸ê®Æ¥H¦r¦ê§Î¦¡¶Ç¦^¡A¦Ó¤£¬Oª½±µ¼g¤JÀɮסC¨Ò¦p¡G

>> str = sprintf('log(%f) = %e\n', 2, log(2)) str = log(2.000000) = 6.931472e-001

¦b¬Y¨ÇÀ³¥Î¤W¡A¥i¯à»Ý­n¥Î¨ì¼È¦s¥Ø¿ý¤Î¼È¦sÀɮסC±ý¨ú¥Î¨t²Îªº¼È¦s¥Ø¿ý¡A¥i¥Î tempdir¡A¦p¤U¡G

>> directory = tempdir directory = C:\Users\ROGERJ~1\AppData\Local\Temp\

±ý¶}±Ò¤@¼È¦sÀɮסA¥i¥Î tempname¡A¦p¤U¡G

>> filename = tempname filename = C:\Users\ROGERJ~1\AppData\Local\Temp\tpd8a0f416_427f_46de_9e3e_c8baf771a03f

Hint
tempdir ©M tempname ©Ò¶Ç¦^ªº¦r¦ê·|¨Ì§@·~¨t²ÎªºÀô¹ÒÅܼƦөw¡A¦]¦¹¤£¦P¨t²Î¥i¯à·|²£¥Í¤£¦Pªº¦^¶Ç¦r¦ê¡C

¥H¤Uªº½d¨Ò¡A´N¬O§Q¥Î tempname ²£¥Í¤@­Ó¼È¦sªº HTML ÀɮסAµM«á¦A±N¦¹ÀÉ®×Åã¥Ü¦bÂsÄý¾¹¡C

Example 2: 18-ÀÉ®×Ū¼g/writeHTML.mfilename = [tempname, '.html']; fid = fopen(filename, 'w'); fprintf(fid, '<html><body>\n'); fprintf(fid, 'This is a test homepage written by MATLAB!\n'); fprintf(fid, '</body></html>'); fclose(fid); dos(['start ', filename]); % ±Ò°Ê©M .html ¬Û³sµ²ªºÀ³¥Îµ{¦¡

²£¥Í¤§ºô­¶¦p¤U¡G

Hint
·íMATLAB ªº­pºâµ²ªG¾A¦X¥Î¦Cªí¡]Table¡^©Î¦h´CÅéÀɮס]¦p¼v¹³¡BÁn­µ¡B°Êµe¡^¨Ó§e²{®É¡A¥Ñºô­¶¨ÓÅã¥Ü³o¨Ç­pºâµ²ªG¬O¤@­ÓÆZ¤£¿ùªº¿ï¾Ü¡C


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