1 / 14

Unit 9 檔案輸入與輸出

Unit 9 檔案輸入與輸出. 中華技術學院電子系 講師 蔡樸生. 開啟檔案. fopen : 開啟檔案 fid=fopen( ‘ filename ’ , ’ permission ’ ) ‘ r ’ : 只准讀取 (reading) ‘ w ’ : 只准寫入 (writing) fopen 傳回一個檔案辨識碼 fid=-1 : 表示檔案不存在 Ex : fid=fopen( ‘ a.m ’ , ’ r ’ ). 關閉檔案. status=fclose(fid); status=0 : 正常關閉檔案

Télécharger la présentation

Unit 9 檔案輸入與輸出

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit 9 檔案輸入與輸出 中華技術學院電子系 講師 蔡樸生

  2. 開啟檔案 • fopen : 開啟檔案 • fid=fopen(‘filename’,’permission’) • ‘r’ : 只准讀取(reading) • ‘w’: 只准寫入(writing) • fopen傳回一個檔案辨識碼 • fid=-1 : 表示檔案不存在 • Ex : fid=fopen(‘a.m’,’r’)

  3. 關閉檔案 • status=fclose(fid); • status=0 : 正常關閉檔案 • status=-1 : 無法順利關閉檔案 讀取ACSII檔案 • feof (fid) : 測試結束指標是否達到結束位置 • fgets (fid) / fgetl (fid) : 讀取一行 ASCII 碼 • fscanf (fid , format) : 讀取格式化資料

  4. EXAMPLE : • fid=fopen('ch9_1.m'); • while feof(fid)==0 • line=fgets(fid); line=fscanf(fid,’%s’) • disp(line); • End • fclose(fid) • % s : 字串 • % d : 10進制整數 • % g : 雙精制的浮點數

  5. EXAMPLE: 製作一ASCII檔案12 23 12 56 78… • fid=fopen('ch9_3.m'); • while feof(fid)==0 • char=fscanf(fid,'%d'); • disp(char); • end • fclose(fid);

  6. 寫入ASCII檔案 • fprintf(‘fid’,’format’,’y’) • fprintf(‘檔案名稱’,’寫入格式’,’變數1,…’) • format : 格式指定字串 • % e : 科學記號 • % f : 固定欄寬 • % g : 字串 • fprintf( ‘A unit circle has circumference %g\n\n’,2*pi) • fprintf( ‘X is %6.2f meter\n’,9.9)

  7. Homework x=1:10; y=[x;sqrt(x)]; fid=fopen('sqrt.dat','w'); fprintf(fid,'Table of Square Root:\n'); fprintf(fid,'%2.0f=>%10.6f\n',y); fclose(fid); 試將sin(t)正弦波分割成36等份,每一等 份為10度. 試將每一值寫入dsin.dat 中

  8. Unit 10 Symbolic Processing 中華技術學院電子系 講師 蔡樸生

  9. Algebraic and Math. Operator • syms : Create Symbolic Variables • expand : expands the expression by carrying out power • simplify : simplifies the expression E • factor : factors the expression E • >> syms x y • >> expand ((x+y)^3) • >> expand (sin(x+y)) • >> factor(x^2-1)

  10. Algebraic and Math. Operator • >> E1=x^2+5; • >> E2=y^3-2; • >> S1=E1+E2 : S1=x^2+3+y^3 • >> S2=E1*E2 : S2=(x^2+5)*(y^3-2) • >> expand(S2) • >> E3=x^3+2*x^2+5*x+10 • >> S3=E3/E1 • >> simplify(S3)

  11. Evaluating Expressions • subs(E,old,new): To replace old with a numeric value new in the expression E • ezplot(E,[xmin xmax]) • poly2sym([a,b,c…….],’v’) • >> syms x • >> E=x^2+6*x+7 • >> G=subs(E,x,2) • >> ezplot(E,[-2 6]) • >> ploy2sym([2,5,-3],’x’)

  12. Test Your Understanding (I) • Given the expressions: • Find the product E1*E2 and express it in its simplest form • Find the quotient E1/E2 and express it in its simplest form • Evaluate the sum E1+E2 at x=7.1

  13. Differentiation • syms n x y • diff(f(x,y),y,n) :對於f(x,y)中的 y 作偏微 n 次 Integration • int(f(x,y),y,a,b) : 對於f(x,y)由 a 到 b 作定積分

  14. Test Your Understanding (II) • Given that ,use MATLAB to find at x=0.2. • Given that ,use MATLAB to find • Given that ,use MATLAB to find • Use MATLAB to evaluate

More Related