MATLAB Session for CS4514 Adrin Her her @wpi.edu Computing & Communictions Center - November 28, 2006- Prt of the notes re from Mtlb documenttion 1 MATLAB Session for CS4514 1. Mtlb Bsics Strting Mtlb Mtlb Help Mtlb Vribles Numbers Opertors Functions Mtrices Writing script Plotting Dt Importing Dt 2. Processing experimentl dt slides 32-end Importing Dt in Mtlb codes: code1.m Processing Dt code2.m Plotting Dt A. Files: typeperf1.csv, typeperf2.csv, etc B. Files: logfile1.dt, logfile2.dt, etc 2
Wht is Mtlb? MATLAB is high-performnce lnguge for technicl computing. It integrtes computtion, visuliztion, nd progrmming in n esy-touse environment where problems nd solutions re expressed in fmilir mthemticl nottion. MATLAB stnds for mtrix lbortory. MATLAB is n interctive system whose bsic dt element is n mtrix (rry) tht does not require dimensioning. This llows you to solve mny technicl computing problems, especilly those with mtrix nd vector formultions, in frction of the time it would tke to write progrm in sclr non-interctive lnguge such s C or Fortrn. 3 Strting Mtlb Windows: Strt menu Mtlb Mtlb Unix: Terminl window type mtlb Menu Current Directory Workspce Window Commnd Window History Window 4
Mtlb Help 1. Using HELP menu MATLAB Help HELP Using Help Browser 2. >> helpdesk Opens the Help browser. 3. >> help commndnme/toolboxnme/functionnme Ex: >> help sin 4. >> doc commndnme/toolboxnme/functionnme displys the detiled info in the Help browser. Ex: >> doc sin Other commnds: 5. >> lookfor = helpdesk -> serch 5 I. Mtlb Progrmming Mtlb Vribles Opertors Functions.. 6
Mtlb Vribles A MATLAB vrible is essentilly tg tht you ssign to vlue in memory. MATLAB does not require ny type declrtions or dimension sttements. When MATLAB encounters new vrible nme, it utomticlly cretes the vrible nd lloctes the pproprite mount of storge. If the vrible lredy exists, MATLAB chnges its contents. Vrible nmes consist of letter, followed by ny number of letters, digits, or underscores. MATLAB uses only the first 31 chrcters of vrible nme. MATLAB is cse sensitive; it distinguishes between uppercse nd lowercse letters. MATLAB stores vribles in prt of memory clled workspce. To view wht is stored in vrible type its nme. Types of Vribles: MATLAB provides three bsic types of vribles: Locl Vribles Globl Vribles Persistent Vribles 7 Mtlb Vribles Rules for vrible nmes: Mke Sure Vrible Nmes Are Vlid Don't Use Function Nmes for Vribles Check for Reserved Keywords Avoid Using i nd j for Vribles How to remove vrible from workspce: Syntx: vrilenme=vlue; Exmple: >> =5; >> b=7; >> c=+b >> method= liner >> cler vriblenme >> cler - removes ll vribles from the workspce (!!!!) ns = defult vrible, when the result is not ssign to vrible Exercise: 1. Define 1=8 nd b2=8, c1=1+b2 2. Other commnds: vrible= input( prompt ) (>>help input) >> 3=input( 3= ) 8
Opertors Expressions use fmilir rithmetic opertors nd precedence rules. 9 Mtlb Functions 1. Stndrd elementry mthemticl functions >> help elfun Trigonometric (sin, cos) Exponentil (exp, log) Complex (bs, ngle) Rounding nd reminder (round) 2. Elementry mtrices nd mtrix mnipultion. >> help elmt 3. Specilized mth functions. >> help specfun 10
Sttistics men, std, min, mx Given: vector x Syntx: M = men(x) S = std(x) min_x = min(x) mx_x=mx(x) - verge or men vlue - returns the stndrd devition - the smllest element in x - returns the lrgest element in x Exmple: open the file mysttistics.m mysttistics.m mysttistics1.m Advnced: Sttistics for multidimensionl dt sets Sttistics toolbox 11 Mtrices Opertors Functions.. 12
Mtrix & bsic mtrix functions Define mtrix: 1. Type the mtrix 2. Use Specilized Mtrix Functions Mtrix Mnipultion Mtrix Functions 13 Mtrix: Define mtrix 1. Type the mtrix Seprte the elements of row with blnks or comms. Use semicolon, ;, to indicte the end of ech row. Surround the entire list of elements with squre brckets, [ ]. 3 S = 10 0 10 0 30 0 30 27 1. >> S=[3-10 0; -10 0 30; 0 30-27] Bsic mtrix informtion: size (size of mtrix) >> [m,n] = size(x) 14
Mtrix: Accessing Mtrix Elements individul element >> A(2,2) ns = 20 1 A = 11 2 20 3 30 column >> A(:,2) row >> A(2,:) group of elements >> A(2,1:3) ns = : (colon) ll elements 2 20 ns = 11 20 30 ns = 11 20 30 first element : step: lst element 15 Mtrix: Opertions A+B A-B A*B A/B A\B A^B A' A.*B A./B A.\B A.^B A.' 16
Mtrix: Opertions 1 2 3 10 20 30 A = 2 3 1, 11 21 31 B =, 2 2 2 1 2 3 >> A=[1 2 3; 2 3 1; 2 2 2]; >> B= [10 20 30; 11 21 31; 1 2 3]; >> A*B ns = 35 68 101 54 105 156 44 86 128 A = 11 21 31 A * B = 11 21 31 12 22 32 b b b 11 21 31 13 23 33, 12 22 32 b b b b B = b b 12 22 32 13 23 33 11 21 31 b b b 13 23 33 b b b 12 22 32 b b b 13 23 33, >> A.*B ns = 10 40 90 22 63 31 2 4 6 17 IV. Mtlb Progrmming How to write progrm (M-files) Script Function How to plot dt 18
M-files Files tht contin code in the MATLAB lnguge re clled M-files. You crete M-files using text editor. Use M-file s ny other MATLAB function or commnd. A M-file is plin text file. Two kinds of M-files: Scripts do not ccept input rguments or return output rguments operte on dt in the workspce. Functions cn ccept input rguments nd return output rguments internl vribles re locl to the function. >> edit filenme >> edit exswitch 19 Mtlb - Plotting plot Sintx: plot(y); plot(x,y); plot(x,y,s) The plot function hs different forms, depending on the input rguments. If y is vector, plot(y) produces piecewise liner grph of the elements of y versus the index of the elements of y. If you specify two vectors s rguments, plot(x,y) produces grph of y versus x. 20
Mtlb - Plotting plot(x,y, s); s llows to plot : colors, symbols, different lines b blue. point - solid g green o circle : dotted r red x x-mrk -. dshdot c cyn + plus -- dshed m mgent * str (none) no line y yellow s squre k blck d dimond. plot (x,y,'c+:') plots cyn dotted line with plus t ech dt point; 21 Mtlb - Plotting cler t=0:0.01:10; % time seconds signlsin=sin(2*pi*t); % signl1 - frequency =1 Hz signlcos=0.5*cos(2*pi*t); % signl2 - frequency =1 Hz figure plot(t,signlsin); hold on plot(t,signlcos, '-*r'); plot2signls.m xlbel('time'); ylbel('signl'); legend('sin', 'Cos'); title('two Signls','FontSize',12) Other commnds: xlbel figure ylbel legend, title 22
Mtlb - Plotting 1 0.8 0.6 Two Signls Sin Cos 0.4 0.2 signl 0-0.2-0.4-0.6-0.8-1 0 1 2 3 4 5 6 7 8 9 10 time 23 Visuliztion - Interctive editing (optionl) show plot tools 24
Visuliztion (optionl) plot, plotyy, stem, subplot plot(y) plot(x1,y1,...) plot(x1,y1,linespec,propertynme',propertyvlue,...) plotyy (X1,Y1,X2,Y2) plots Y1 versus X1 with y-xis lbeling on the left nd plots Y2 versus X2 with y-xis lbeling on the right. stem (X,Y) plots the dt sequence Y t the vlues specified in X. suplot (m,n,p), breks the Figure window into n m-by-n mtrix of smll xes, selects the p-th xes for for the current plot 25 Visuliztion %%%%% plot commnds %%%%%%%%%%%%%%% t=1:1:20; x=sin(t/5); y=0.01*x.*exp(-t); see: testplot.m figure subplot(2,2,1) plot(t,x,'--rs','linewidth',2, MrkerEdgeColor','k',... 'MrkerFceColor','g','MrkerSize',6); title('plot 1'); subplot(2,2,2) stem(t,x,'--rs','linewidth',2); title('plot 2') subplot(2,2,3); br(t,x); title('plot 3') subplot(2,2,4) plotyy(t,x, t,y); title('plotyy') 26
Visuliztion (optionl) Line Plots Br Plots 3D Surfce Vector Fields 27 Publishing script to HTML 28
Importing nd Exporting Dt using the Import Wizrd sve, lod dlmred, dlmwrite xlsred, xlswrite fopen,, fscnf, fprintf importdt 29 Importing Dt 1. Using the Import Wizrd with Text Dt File Import Dt or >> uiimport 2 Supported File Formts Wizrd: missing dt: NN (Not--Number.) 30
Importing nd Exporting Dt 2 Supported File Formts 31 Processing Experimentl Dt for the Project 1. Dt Preprtion 2. Import Dt in Mtlb 3. Process Dt 4. Plot Dt A. Files: typeperf1.csv, typeperf2.csv, etc B. Files: logfile1.dt, logfile2.dt, etc code1.m code2.m 32
1. Dt Preprtion 1. Open s Text (typeperf.csv) 2. Editor window: Edit Find nd Replce Remove double quottion mrks 3. File Sve s (typeperfm.csv) 33 2. Import Dt in Mtlb A = importdt( typeperfm.csv ); % lod dt into structure Structure A with two fields: A.dt (numeric dt) A.textdt (text dt) 34
2. Import Dt in Mtlb A = importdt( typeperfm.csv ); Col 1 Col 2 Col 3 Col 4 A.textdt (text dt) 35 3. Process Dt min sec msec timedte=a.textdt(2:end,1); % time dt timeutc=dtenum(timedte); % trnsform time to UTC relutctime=timeutc-timeutc(1); % define reltive time T=dtestr(relUTCTime, 'MMSSFFF'); % time in minutes, seconds, mseconds 36
4. Plot Dt 40 30 Figure1 typeperf1m.csv typeperf2m.csv col 3 20 10 0 20 40 60 80 100 120 140 160 180 Time 1.1 x 107 Figure2 1.1 col 4 1.1 1.1 1.1 0 20 40 60 80 100 120 140 160 180 Time Figure3 80 60 col 8 40 20 0 0 20 40 60 80 100 120 140 160 180 Time 37 1. Dt Preprtion 1. Open s Text (logfile.dt) 2. Editor window: Edit Find nd Replce Remove [00-01-36-0D-CE-5D] 3. File Sve s (logfilem.dt) 38
2. Import Dt in Mtlb Row no dtexp=dlmred('logfilem.dt', '', 1, 0); Col no 3. Process Dt timeutc=dtexp(:,1); reltime=timeutc-timeutc(1); 39 3. Plot Dt & Extrct Info 40
Processing Experimentl Dt for the Project 1. Dt Preprtion 2. Import Dt in Mtlb 3. Process Dt 4. Plot Dt A. Files: typeperf1.csv, typeperf2.csv, etc B. Files: logfile1.dt, logfile2.dt, etc code1.m code2.m 41