330 likes | 546 Views
بسم الله الرّحمن الرّحيم. آموزش نرم افزار MATLAB. علی دانش گروه رباتیک دانشگاه پیام نور. چند نکته. امکان کامنت چندخطی (بلوکی) در نسخه 7 %{ %} امکانات دیباگ کردن امکان Cell Mode در Script نویسی (نسخه 7) دموهای جالب و مفید demo. توابع تو در تو ( nested function ).
E N D
بسم الله الرّحمن الرّحيم آموزش نرم افزارMATLAB علی دانش گروه رباتیک دانشگاه پیام نور
چند نکته • امکان کامنت چندخطی (بلوکی) در نسخه 7 %{ %} • امکانات دیباگ کردن • امکان Cell Mode در Script نویسی (نسخه 7) • دموهای جالب و مفید demo
توابع تو در تو (nested function) • اشتراک گذاری داده ها • ساختن توابع سفارشی • ساختن توابع سفارشی با داشتن حالت داخلی
به اشتراک گذاشتن داده ها >> type taxDemo.m function y = taxDemo(income) % Calculate the tax on income: AdjustedIncome = income - 6000; y = computeTax; function y = computeTax % Can see the variable 'AdjustedIncome' y = 0.28 * AdjustedIncome; end end
ساختن توابع سفارشی >> type makefcn.m function fcn = makefcn(a, b, c) % Return handle to nested function: fcn = @parabola; function y = parabola(x) % Can see the variables 'a','b', and 'c' y = a * x .^ 2 + b .* x + c; end end
[ادامه] توابع سفارشی • f = makefcn(3, 2, 10); • g = makefcn(0, 5, 25); • این دو handle به دو تابع متفاوت (ضرایب متفاوت) اشاره می کنند که می توان مقدار هر کدام را محاسبه کرد و یا به عنوان پارامتر به توابع دیگر منتقل کرد. • y = f(2) % = 26 • y = g(2) % = 35 • minimum = fminbnd(f, -5, 5); % = -0.3333
[ادامه] توابع سفارشی ezplot(f); % Plot f over a range of x hold on; plot(2, f(2), 'd'); % Plot a marker at (2, f(2)) plot(minimum, f(minimum), 's'); text(minimum, f(minimum) - 2, 'Minimum'); h = ezplot(g); set(h, 'color', 'red') % Plot g over a range of x plot(2, g(2), 'rd'); % Plot a marker at (2, g(2)) hold off;
ساختن توابع سفارشی با داشتن حالت داخلی >> type makecounter.m function countfcn = makecounter(initvalue) currentCount = initvalue; countfcn = @getCounter; function count = getCounter % Increments the 'currentCount‘ currentCount = currentCount + 1; count = currentCount; end end
[ادامه] توابع سفارشی با حالت داخلی >> counter1 = makecounter(0); >> counter2 = makecounter(10); >> counter1 % = 1 >> counter1 % = 2 >> counter2 % = 11 >> counter1 % = 3 >> counter2 % = 12
خواندن فایلهای متنی با فرمت دلخواه • load % Variables from .MAT file • fread(.) % StrRead(.) for string • fscanf(.) % SScanf(.) for string • fgets(.) % newline included • fgetl(.) % newline NOT included • textread(.) % Fixed Format • textscan(.) % Variable Format
تابع textscan(.) • تنظیم فرمت فایل • باز کردن فایل متنی برای خواندن • خواندن خطوط مقدماتی • خواندن بلوکها • بستن فایل متنی • تعداد بلوکهای خوانده شده • بررسی داده ها
مثال: فایل متنی test80211.txt • بعد از 4 خط مقدمه، فایل از یک بلوک داده تشکیل شده است که به فرمت زیر می باشد: • دو خط توضیحات • پارامتر m • جدول داده ها به شکل p * m • تمام داده های خوانده شده در آرایه سلولی قرار داده می شود.
[ادامه] مثال fid = fopen('test80211.txt', 'r'); % Open text file % Read strings delimited by a carriage return InputText = textscan(fid, '%s', 4, 'delimiter', '\n'); Intro = InputText{1}; disp(Intro);
[ادامه] مثال – خواندن بلوک • خواندن هر بلوک: • خواندن عنوان • خواندن نام جدول • خواندن سرستون • خواندن داده ها
[ادامه] مثال – کد Block = 1; % Initialize block index while (~feof(fid)) % For each block... % Read header line InputText = textscan(fid, '%s', 2, 'delimiter', '\n'); HeaderLines{Block, 1} = InputText{1}; InputText = textscan(fid, 'Num SNR=%f'); % Read parameter value NumCols = InputText{1}; % Read data block: FormatString = repmat('%f', 1, NumCols); % Create format string InputText = textscan(fid, FormatString, 'delimiter', ','); % Convert to numerical array from cell: Data{Block, 1} = cell2mat(InputText); [NumRows, NumCols] = size(Data{Block}); % Size of table % Read and discard EOB marker ('EOF' in this case): eob = textscan(fid, '%s', 1, 'delimiter', '\n'); Block = Block + 1; % Increment block index end
[ادامه] مثال – کد 2 fclose(fid); NrOfBlocks = Block - 1 % How many blocks ? % Display Block #9 : Block = 9; disp(HeaderLines{Block}); disp(['SNR' sprintf(' %d', Data{Block, 1}(1, 2:end))]) format short e % Use exponential format disp(' '); disp(Data{Block, 1}(2:end, 2:end)); '* Indoor0' '* SNR Vs test No' SNR -7 -6 9.0600e-007 6.7100e-007 3.1700e-007 3.5400e-007 2.8600e-007 1.9600e-007 1.4800e-007 7.3400e-007 3.9500e-008 9.6600e-007 7.9600e-007 7.8300e-007 4.0000e-007 8.8100e-007 3.0100e-007 2.9700e-007
نمایش نمودار دو بعدی XY plot
تابع plot x = 0:0.05:5; Y = sin(x .^ 2); plot(x, y);
تابع bar x = -2.9:0.2:2.9; bar(x, exp(-x .* x));
تابع stairs x = 0:0.25:10; stairs(x, sin(x));
تابع errorbar x = -2:0.1:2; y = erf(x); e = rand(size(x)) / 10; errorbar(x, y, e);
تابع polar t = 0:.01:2*pi; polar(t, abs(sin(2 * t) .* cos(2 * t)));
تابع stem x = 0:0.1:4; y = sin(x .^ 2) .* exp(-x); stem(x, y)
مثال – رسم سری فوریه t = 0:.1:10; y = sin(t); plot(t, y); y = sin(t) + sin(3 * t) / 3 + sin(5 * t) / 5 + sin(7 * t) / 7 + sin(9 * t) / 9; plot(t, y);
[ادامه] مثال – رسم سری فوریه t = 0:.02:3.14; y = zeros(10, length(t)); x = zeros(size(t)); for k=1:2:19 x = x + sin(k * t) / k; y((k + 1) / 2, :) = x; end plot(y(1:2:9, :)') title('The building of a square wave: Gibbs'' effect')
[ادامه] مثال – رسم سری فوریه surf(y); shading interp axis off ij
نمایش نمودار سه بعدی XYZ plot
توابع رسم سه بعدی z=peaks(25); %%% mesh(z); colormap(hsv) surf(z); colormap(jet); surfl(z); shading interp; colormap(pink); contour(z,16); colormap(hsv)
مقدمه تصویر تصاویر و ماتریس ها
انواع تصویر • Black & White (B/W) • Grayscale • Colorful • Color Index (PALETTE) • Color Value • RGB (additive) • CMYK (subtractive) • HSI (HSV) (Hue Saturation Intensity) • Ia*b* • …
تابع image(.) و colormap(.) X = spiral(8); image(X); colormap(gray); %%% colormap(hsv); %%% colormap(hot); 43 44 45 46 47 48 49 50 42 21 22 23 24 25 26 51 41 20 7 8 9 10 27 52 40 19 6 1 2 11 28 53 39 18 5 4 3 12 29 54 38 17 16 15 14 13 30 55 37 36 35 34 33 32 31 56 64 63 62 61 60 59 58 57
تابع rgbplot(.) برای مشاهده نمودار نگاشت رنگ: rgbplot(S)
توابع خواندن و نوشتن تصویر imread