II MATLAB Chapter 8 MFile Programming II Chapter

응용전산 및 실습 II MATLAB – Chapter 8 M-File Programming II 위덕대학교 에너지전기공학부 이성환 Chapter 8 Uiduk University



![MATLAB – Quest. Dlg Example EX) line_color=['r', 'g', 'b']; str='Yes'; t=-2*pi: 1/100: 2*pi; y=sin(t); MATLAB – Quest. Dlg Example EX) line_color=['r', 'g', 'b']; str='Yes'; t=-2*pi: 1/100: 2*pi; y=sin(t);](http://slidetodoc.com/presentation_image_h2/d1012c19ff84a6bb37f5b8f3d2e90b66/image-5.jpg)
MATLAB – Quest. Dlg Example EX) line_color=['r', 'g', 'b']; str='Yes'; t=-2*pi: 1/100: 2*pi; y=sin(t); while(1) if strcmp(str, 'Yes‘) k=menu('선의 색을 결정', 'red', 'green', 'blue'); plot(t, y, line_color(k)) str=questdlg('계속할까요? ', '진행상태', 'Yes', 'No', ‘No'); else break; end close all Chapter 8 Uiduk University



MATLAB – Selective Statement (Switch-Case) EX) k = input('k 값은 ? '); y = rem(k, 2) switch(y) case 0 disp(‘짝수가 입력되었습니다. '); case 1 disp(‘홀수가 입력되었습니다. '); end Chapter 8 Uiduk University

MATLAB – Subfunction EX) Calc. Sub. m % Calculate 1 + (1 x 2) + (1 x 2 x 3). . . (1 x 2 x 3 x … x n) % Main Function function ret = Calc. Sub(n) ret = 0; for k=1 : n ret = ret + Mult. To. N(k); end % Sub Function function mtot = Mult. To. N(n) mtot = 1; for k=1 : n mtot = mtot * k; end Chapter 8 Uiduk University

MATLAB – Function with Array Input length(A) - Get the item count of array A EX) >> A = [ 2, 4, 6, 8, 10 ]; >> length(A) ans = 5 Chapter 8 Uiduk University

MATLAB – Function with Array Input EX) Array. Average. m function avg = Array. Average(A) Total. Item = length(A); Sum = 0; for i = 1 : Total. Item Sum = Sum + A(i); end avg = Sum / Total. Item; Chapter 8 Uiduk University
![MATLAB – Function with Multiple Output function [A, B, …] = function_name(a, b, …) MATLAB – Function with Multiple Output function [A, B, …] = function_name(a, b, …)](http://slidetodoc.com/presentation_image_h2/d1012c19ff84a6bb37f5b8f3d2e90b66/image-12.jpg)
MATLAB – Function with Multiple Output function [A, B, …] = function_name(a, b, …) EX) Min. Max. m function [min, max] = Min. Max(A) Total. Item = length(A); max = 0; min = 10000; for i = 1 : Total. Item if max < A(i) max = A(i); end if min > A(i) min = A(i); end Chapter 8 Uiduk University

MATLAB – Function with Multiple Output EX) >> A = [ 4, 6, 2, 8, 16] A= 4 6 2 8 16 >> [m, M] = Min. Max(A) m= 2 M= 16 Chapter 8 Uiduk University
- Slides: 13