Lecture 3 David S. Touret zky March 2001 Mar ch 29, 2001 3:08 am 2 oI 22 Multidimensi onal Arrays MATLABsuppor t s ar r ays with more t han two dimensi ons. m r and(4, 3, 2) whos m si ze(m) Mat ri x mul t i pl i cat i on does not appl y to hi gher di mensi onal arrays. Array concat enat i on can`t be done with| | not at i on Ior mor e t han 2 di mensi ons, so use cat(di m,A,B) i nst ead. cat (1,m,m) cat (2, m,m) cat (3, m,m) Mar ch 29, 2001 3:08 am 3 oI 22 Sparse Matri ces Spar se mat ri ces provi de an eIfcent means t o st ore mat ri ces t hat are mostly empty. s spar se(1000, 1000) s(2, 11) 3.5 s(875, 992) 47 s(992,875) 2 whos s All arithmet i c operat i ons wor k on spar se as we ll as Iull mat ri ces. s 10 s` *s Mar ch 29, 2001 3:08 am 4 oI 22 Cell Arrays Ordi nary arrays cont ai n el ement s oI a si ngl e type, such as doubl es or char s. Cel l arrays cont ai n obj ect s oI het erogeneous types. b {1 `t wo` | 3 3 3| } Cel l arrays oIIer great er fexibility. Drawback: cel l ar r ays requi re much more st orage, because each el ement requi res a separat e poi nt er and t ype i nIorma t i on. r a | 1 2 3 4| ; ca {1 2 3 4}; whos r a ca r a(2:3) ca{2: 3} Mar ch 29, 2001 3:08 am 5 oI 22 Structure Arrays MATLABsuppor t s 'st ruct ure arrays with named fel ds. a. name ` John Smi t h` a. age 35 a. depart ment `Account i ng` whos a The ar r ay a i s a scal ar (1x1) st ruct ure array. I fe l dnames(a) I {1} The what and get Iunct i ons ret urn st ruct ures. w what w.m p get (gcI ) Mar ch 29, 2001 3:08 am 6 oI 22 St ruct ure Arrays (cont. ) St ruct ure arrays can cont ai n mor e t han one el ement. Al l el ement s have t he same set oI fel d names; some fel ds may be empt y. a(2). name ` Mar y Brown` a(2).seni ori t y 8 whos a a(1) a(2) Mar ch 29, 2001 3:08 am 7 oI 22 Call-by-Value Semanti cs MATLABuses cal l -by-val ue semant i cs, maki ng i t i mpossi bl e Ior Iunct i ons t o modi Iy t hei r argument s. In C, i nt egers and real s are passed by val ue, but arrays ar e passed by reIerence, maki ng t hem modi fabl e. The Iol l owi ng doesn`t wor k: bi rt hday(a(1)) a(1) Iunct i on bi rt hday(emp) emp. age emp. age 1 Mar ch 29, 2001 3:08 am 8 oI 22 Return Modied Structures Modi fed ar r ays or st ruct ures must be ret urned, and assi gned back t o t he ori gi nal vari abl e. Ot herwi se t he modi fcat i ons are l ost . a(1) a(1) bi rt hday(a(1)); a(1) Iunct i on emp bi rt hday(emp) emp. age e mp. age 1; Mar ch 29, 2001 3:08 am 9 oI 22 Efci ency Considerations When an ar r ay or st ruct ure i s passed as an ar gument , MATLABdoesn`t necessar i l y copy it. Obj ect s are passed t o Iunct i ons by reIerence, but are copi ed i I t he Iunct i on modi fes t he ar gument. The modi Iy- and- ret urn approach i s not an eIfci ent wa y t o mai nt ai n l arge obj ect s, due t o excessi ve copyi ng. Al t ernat i ve sol ut i on: st ore val ues i n a gl obal vari abl e and l et Iunct i ons modi Iy t hat vari abl e di rect l y. Mar ch 29, 2001 3:08 am 10 oI 22 Property Arguments The pl ot Iunct i on and many ot her graphi cs Iunct i ons accept propert y/val ue pai rs as ext ra ar gument s. cl I, hol d on pl ot (rand(5, 1), `Col or`, |0 0. 5 0. 8|) pl ot (rand(5,1),`r`,`Li neWidth`,8, ... `Li neSt yle`,`- - `) pl ot (rand(5, 1), `Col or`, |0. 7 0. 9 0. 2|, . . . `Li neWi dt h`, 6, `Li neSt yl e`,```) Mar ch 29, 2001 3:08 am 11 oI 22 GUI Facility UIcont rol obj ect s i ncl ude pusbut t ons, sl i ders, pop- up menus, and r adi o but t ons. clI hb ui cont rol (`St yl e`, `pushbut t on`) set (hb, `St ri ng`, `Eoo`) set (hb, `Backgr oundCol or`, |0.2 0.6 1|) set (hb, `Cal l back`, `dat est r(now)`) set (hb, `St yl e`, `radi obut t on`) Mar ch 29, 2001 3:08 am 12 oI 22 Units Property The Uni t s propert y cont rol s whet her cert ai n subsequent propert i es are i nt erpret ed as pi xel s, poi nt s, or percent age oI screen si ze ('nor mal i zed uni t s). set (hb,`Uni t s`,`pi xel s`, ... `Posi t i on`, |100 100 80 25|) set (hb,`Uni t s`,`normal i zed`, ... `Posi t i on`, |0. 5 0. 5 0. 25 0. 25|) Mar ch 29, 2001 3:08 am 13 oI 22 Pop-up Menus and List Boxes hp ui cont r ol (`St yl e`,`popup`, ... `St ri ng`, {`eeny`,`meeny`,`mi ney`,`moe`}, ... `Uni t s`,`normal i zed`, ... `Posi t i on`,|0.2 0.2 0.3 0.1|, ... `BackgroundCol or`, |0. 8 0. 8 0. 5|, . . . `Eor egr oundCol or `, | 0. 5 0. 5 0. 2| ) set (hp,`Cal l back`,`get (gcbo,``Val ue``)`) The gcbo Iunct i on ret urns t he obj ect whose cal l back Iunct i on i s current l y execut i ng. set (hp, `Posi t i on`, |0. 2 0. 2 0. 3 0. 2|) set (hp,`St yl e`,`l i st box`) Mar ch 29, 2001 3:08 am 14 oI 22 Sliders hs ui cont rol (`St yle`,`slider`, ... `Posi t i on`, |200 200 150 20|, . . . `Cal l back`,`get (gcbo,``val ue``)`) set (hs,`CallBack`, . . . `set (gcI,``Col or``,|0 0 get (gcbo,``Val ue``)|)`) In pract i ce, t he Cal l back st ri ng i s usual l y a cal l t o some user-wri t t en Iunct i on with gcbo as t he argument . Al l t he work i s done i nsi de t he Iunct i on. Mar ch 29, 2001 3:08 am 15 oI 22 GUIDE GUI DE i s t he GUI Des i gn Envi r onment . Allows i nt eract i ve l ayout oI a GUI wi ndow, i ncl udi ng menus, graphi cs, t ext boxes, et c. hel p gui de Creat es a . fg fl e t o st ore l ayout i nIormat i on. Creat es an edi t abl e .m fl e t o l oad t he . fg fl e and hol d associ at ed cal l back rout i nes. Mar ch 29, 2001 3:08 am 16 oI 22 Image Data cl ear al l l oad dur er whos image(X) col ormap(map) axi s i mage axi s oII set (gca, `Posi t i on`, |0 0 1 1|) col ormap(aut umn) bri ght en(0. 5) Mar ch 29, 2001 3:08 am 17 oI 22 Reading Image Files imfnIo(`/aIs/cs/usr/dst /brai n.j pg`) brai n imread(`/aIs/cs/usr/dst /brai n.j pg`); whos br ai n The ui nt 8 dat at ype hol ds unsi gned byt es. image(br ai n) col ormap(bone) axi s i mage col ormap(bone(256)) zoom on Mar ch 29, 2001 3:08 am 18 oI 22 Mous e Input get l i ne(`cl osed`) Cl i ck t he l eIt but t on t o ent er poi nt s. Cl i ck t he ri ght mouse but t on t o end. Ret ur n val ue i s a mat ri x oI poi nt s defni ng t he pol ygon. p1 get pt r (gcI ) set pt r(gcI,`hand`) p2 get pt r(gcI) set (gcI, p1{:}) The p{:} not at i on expands t he cont ent s oI t he cel l array p i nt o mul t i pl e argument s t o set. Mar ch 29, 2001 3:08 am 19 oI 22 Image Manipulation Iunct i on bmap(i m) cl I, zoom on image(i m), axi s i mage, axi s oII d doubl e(i m); m | bone(256);aut umn(50)| ; col ormap(m) coords get l i ne(`cl osed`); |x,y| meshgri d(1:si ze(i m,1), ... 1:si ze(i m, 2)); z i npol ygon(x, y, ... coor ds(:, 1), coor ds(:, 2)); d(z) d(z) 50; image(d), axi s i mage, axi s oII Mar ch 29, 2001 3:08 am 20 oI 22 Toolboxes Tool boxes cont ai n col l ect i ons oI rel at ed Iunct i ons t hat ext end t he basi c MATLAB l anguage. The matl ab t ool box cont ai ns a vari et y oI l i brari es t hat i mpl ement MATLAB I eat ures such as gr aphi ng and mat ri x Iunct i ons. hel p gr aph2d hel p mat Iun The images and stats t ool boxes cont ai n rout i nes Ior i mage processi ng and st at i st i cal cal cul at i on. The ver command di spl ays versi on i nIorma- t i on Ior al l t he t ool boxes current l y i nst al l ed. ver Mar ch 29, 2001 3:08 am 21 oI 22 Search Path You can cont r ol t he sear ch pat h MATLAB uses t o fnd I unct i ons and dat a fl es. pat h addpat h(`/aIs/cs/usr/dst /bump`) what bump bump Mar ch 29, 2001 3:08 am 22 oI 22 Thinking in MATLAB pt s 0:pi / 160:12*pi ; dat a si n(pt s) si n(0. 3*pt s) cos(3*pt s); cl I, hol d on, pl ot (dat a) t hresh mean(dat a) var(dat a); b dat a > t hr esh; bst art ~b(1: end-1) & b(2:end); bend b(1: end-1) & ~b(2: end); pst art fnd(bst art ); pend 1fnd(bend); xcoor ds |pst art ; pend; NaN*pst art |; ycoords repmat (t hresh, si ze(xcoords)); pl ot (xcoords(:), ycoords(:), `r`, `Li newi dt h`,2)