18-1 �������ɮ�Ū�g

MATLAB ¦³³\¦h²{¦¨ªº«ü¥O¡A¥i¥H¤ä´©¦UºØ±`¨£Àɮ׮榡ªºÅª¼g¡A§Ú­Ì±N¸û­«­nªºÀɮ׮榡¤Î¬ÛÃöŪ¨ú¡]©Î¼g¤J¡^«ü¥O¾ã²z¦p¤U¡]¥H MATLAB 8.5 ¬°¥D¡^¡G

Ãþ§O¤ä´©Àɮ׮榡«ü¥O
¤@¯ë¸ê®Æ MAT - MATLAB workspaceload, save
CSV - Comma separated numbers csvread, csvwrite
DAT - Formatted text importdata
DLM - Delimited text dlmread, dlmwrite
TAB - Tab separated text
¸Õºâªí XLS - Excel worksheet xlsread
¬ì¾Ç¸ê®Æ CDF - Common Data Format cdfread, cdfwrite
FITS - Flexible Image Transport System fitsread
HDF - Hierarchical Data Format hdfread, hdfwrite
¹q¼v AVI, MPG, WMV, ASF, ASX, MP4, M4V, MOV, etc.videoreader
¼v¹³ TIFF, PNG, HDF, BMP, JPEG, GIF, PCX, XWD, CUR, ICO, RAS, PBM, PGM, PPM, etc. imread, imwrite
­µ°T WAV, OGG, FLAC, AU, MP#, M4a, MP4, AU, SND, etc. audioread, audiowrite

¥H¤U±N°w¹ï´X­Ó±`¥Îªº«ü¥O¨Ó»¡©ú¡C

¦pªG¤@­Ó¸ê®ÆÀɮ׬O¥Ñ³r¸¹¤À¶}¡A¦Ó¥B¥u¦³¥]§t¼Æ­È¸ê®Æ¡A«h¥i¥H¨Ï¥Î csvread «ü¥O¨ÓŪ¨ú¦¹ÀɮסA¨Ò¦p¡G

Example 1: 18-ÀÉ®×Ū¼g/csvread01.mfprintf('data.csv ªº¤º®e¡G\n'); type data.csv % ¦C¥X data.csv ªº¤º®e A = csvread('data.csv') % ±N data.csv ªº¤º®eŪ¨ì¯x°} Adata.csv ªº¤º®e¡G 1, 2, 3 4, 5 6, 7, 8, 9 A = 1 2 3 0 4 5 0 0 6 7 8 9

¥Ñ¤W­z½d¨Ò¥iª¾¡Acsvread ·|¶Ç¦^¤@­Ó¼Æ­È¯x°}¡A¨Ã±N¯Ê®uªº¸ê®Æ¥H 0 ¶ñ¤J¡C

¦pªG¼Æ­È¸ê®Æªº¤À¬É²Å¸¹¡]Delimiters¡^¤£¬O³rÂI¡A´N¤£¯à¨Ï¥Î csvread «ü¥O¡A¦Ó­n§ï¥Î dlmread «ü¥O¡A¨Ò¦p¡G

Example 2: 18-ÀÉ®×Ū¼g/dlmread01.mfprintf('data.dlm ªº¤º®e¡G\n'); type data.dlm % ¦C¥X data.dlm ªº¤º®e A = dlmread('data.dlm', '\t') % ±N data.dlm ªº¤º®eŪ¨ì¯x°} Adata.dlm ªº¤º®e¡G 1 2 3 4 5 6 7 8 9 A = 1 2 3 0 4 5 0 0 6 7 8 9

¦b¤W­z½d¨Ò¤¤¡Adata.dlm ªº¸ê®Æ¬O¥H©w¦ìÁä¡]Tab¡^¹j¶}¡A¦]¦¹ dlmread «ü¥Oªº²Ä¤G­Ó¤Þ¼Æ¬O ¡¥\t¡¦¡A¥H¥Nªí©w¦ìÁä¡C

¦pªGÀɮ׸ê®Æ¥]§t¼Æ­È¤Î¦r¦ê¡A§Ú­Ì´N¥²¶·§ï¥Î textread «ü¥O¡A¨Ò¦p¡G

Example 3: 18-ÀÉ®×Ū¼g/textread01.mfprintf('data.txt ªº¤º®e¡G\n'); type data.txt % ¦C¥X data.txt ªº¤º®e [name, hobby, age] = textread('data.txt', '%s%s%d')data.txt ªº¤º®e¡G Timmy OnlineGames 13 Annie Chatrooms 10 Roger Tennis 41 name = 'Timmy' 'Annie' 'Roger' hobby = 'OnlineGames' 'Chatrooms' 'Tennis' age = 13 10 41

¦b¤W­z½d¨Ò¤¤¡Adata.txt ¥]§t¤T­ÓÄæ¦ì¡]©Î¬O¤Tª½¦æªº¸ê®Æ¡^¡Atextread ¥i¥H¦b²Ä¤G­Ó¤Þ¼Æ«ü©w¸ê®Æ«¬ºA¡]¨Ò¦p %s ¥Nªí¦r¦ê¡A%d ¥Nªí¾ã¼Æ¡^¡A¨Ã±NŪ¤Jªº¸ê®Æ³]©w¨ì¤£¦Pªº¿é¥X¤Þ¼Æ¡A¥Ñ©ó¸ê®Æ«¬ºAªº¤£¦P¡A¿é¥X¤Þ¼Æ¤]¦³¤£¦Pªº«¬ºA¡A¥H¤W­z½d¨Ò¨Ó»¡¡Aname ©M hobby ³£¬O¦r¦ê²§­È°}¦C¡A¦Ó age «h¬O¼Æ­È°}¦C¡C

¦b¤W¨Ò¤¤¡A§Ú­Ì¬O§Q¥ÎªÅ®æ¨Ó¤À¹jÄæ¦ì¡A¦pªGÄæ¦ì­È¥»¨­¤]¦³ªÅ®æ¡A§Ú­Ì´N¥i¥H¦Ò¼{¨Ï¥Î©w¦ìÁä¨Ó¤À¹jÄæ¦ì¡C¨Ò¦p¡G

Example 4: 18-ÀÉ®×Ū¼g/textread02.mfprintf('data2.txt ªº¤º®e¡G\n'); type data2.txt % ¦C¥X data2.txt ªº¤º®e [name, hobby, age] = textread('data2.txt', '%s%s%d', 'delimiter', '\t')data2.txt ªº¤º®e¡G Timmy Online Games 13 Annie Chatrooms 10 Roger Table Tennis 41 name = 'Timmy' 'Annie' 'Roger' hobby = 'Online Games' 'Chatrooms' 'Table Tennis' age = 13 10 41

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¥u­n¦b textread «ü¥O¤¤¦A¥[¤W¹ï©ó¤À¬É¦r¤¸¡]Delimiters¡^ªº©w¸q¡A´N¥i¥H¥¿½T¦aŪ¥X¨Ó¥Ñ©w¦ìÁä©Ò¤À¹jªº¸ê®ÆÀɮסC

¨Ï¥Î textread «ü¥O¡A§Ú­Ì¤]¥i¥HŪ¨ú¤@­Ó¤å¦rÀÉ¡A¦P®É§âÀɮפºªº¨C¤@¦C¤å¦rÅܦ¨¦r¦ê²§½è°}¦C¸Ì­±ªº¨C¤@­Ó¤¸¯À¡A¨Ò¦p¡G

Example 5: 18-ÀÉ®×Ū¼g/textread03.mcontents = textread('textread03.m','%s','delimiter','\n','whitespace',''); class(contents) % ¦L¥X contents ªº¸ê®ÆÃþ§O contents{1} % ¦C¥X contents ²Ä¤@¦C contents{2} % ¦C¥X contents ²Ä¤G¦C ans = cell ans = contents = textread('textread03.m','%s','delimiter','\n','whitespace',''); ans = class(contents) % ¦L¥X contents ªº¸ê®ÆÃþ§O

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¨Ï¥Î textread «ü¥O¨ÓŪ textread03.m¡]¤]´N¬O¦¹½d¨ÒÀɮס^¡A¨ÃÅã¥Ü¦¹Àɮתº²Ä¤@¦C©M²Ä¤G¦C¡C

¨Æ¹ê¤W¡Atextread «ü¥Oªº¥Îªk«Ü¦h¡A¥\¯à¤]«Ü±j¤j¡AŪªÌ¥i¥H¿é¤J¡uhelp textread¡v´N¥i¥H±o¨ì§¹¾ãªº§Þ³N¤ä´©¡C

¦pªG¥u¬O­n±N¤@­ÓÀɮתº¤º®e°e¨ì¤@­Ó¦r¦ê¡A¥i¥H¨Ï¥Î fileread «ü¥O¡A½d¨Ò¦p¤U¡G

Example 6: 18-ÀÉ®×Ū¼g/fileread01.mout = fileread('data2.txt'); class(out) size(out) ans = char ans = 1 64

¦b¤W­z½d¨Ò¤¤¡Afileread «ü¥O·|±NÀÉ®× data2.txt ªº¤º®e°e¨ì¦r¦êÅÜ¼Æ out¡Aclass(out) ªº­È¬O char¡A Åã¥Ü out ªº¸ê®Æ«¬ºA¬O¦r¦ê¡Asize(out) «hÅã¥Ü¦r¦ê out ªºªø«×¬O 64¡C

¦³Ãö©ó¼v¹³ÀɮתºÅª¼g¡AŪªÌ¥i¥H°Ñ¦Ò¥»®Ñ²Ä¤Q¤E³¹¡u¼v¹³Åã¥Ü»PŪ¼g¡v¡F¦³Ãö­µ°TÀɮתºÅª¼g¡AŪªÌ¥i¥H°Ñ¦Ò¥»®Ñ²Ä¤G¤Q³¹¡u­µ°TŪ¼g¡B¿ý»s»P¼½©ñ¡v¡C

¦pªG§Aªº¸ê®ÆÀɮײŦX¥»¤p¸`©Ò»¡©úªº®æ¦¡¡A¨º´NºÉ¶q±Ä¨ú¥»¤p¸`©Ò¤¶²Ðªº«ü¥O¨Ó¶i¦æŪ¼g¡C¦ý¬O¦pªG§Aªº¸ê®Æ®æ¦¡¤ñ¸ûÁc½Æ¡A¨º´N»Ý­n¤@¦C¤@¦CŪ¶i¨Ó¡A¦A¶i¦æ­åªR¡]Parsing¡^¡A¬ÛÃö«ü¥O±N¦b¤U¦C¦U¤p¸`¤¶²Ð¡C


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