Danh mục

Lecture note Data visualization - Chapter 27

Số trang: 34      Loại file: pptx      Dung lượng: 2.84 MB      Lượt xem: 13      Lượt tải: 0    
Thu Hiền

Hỗ trợ phí lưu trữ khi tải xuống: 15,000 VND Tải xuống file đầy đủ (34 trang) 0
Xem trước 4 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

The main contents of the chapter consist of the following: Function M-files, syntax of function M-Files, comments, multiple input and output functions, relational model concepts, relational model constraints and relational database schemas, update operations and dealing with constraint violations.
Nội dung trích xuất từ tài liệu:
Lecture note Data visualization - Chapter 27Lecture27RecapFunctionMfilesSyntaxofFunctionMFilesCommentsMultipleInputandOutputFunctionsFunctionswithNoInputorNoOutputAlthoughmostfunctionsneedatleastoneinputand returnatleastoneoutputvalue,insomesituationsno inputsoroutputsarerequiredForexample:considerthisfunction,whichdrawsastar inpolarcoordinates: function[]=star() theta=pi/2:0.8*pi:4.8*pi; r=ones(1,6); polar(theta,r)ThesquarebracketsonthefirstlineindicatethattheContinued….TherearenumerousbuiltinMATLABfunctionsthatdo notrequireanyinput.Forexample: A=clock returnsthecurrenttime: A= 1.0e+003* Columns1through4 2.0050 0.0030 0.0200 0.0150 Columns5through6DeterminingtheNumberofInputandOutputArgumentsTheremaybetimeswhenyouwanttoknowthenumber ofinputargumentsoroutputvaluesassociatedwitha functionMATLABprovidestwobuiltinfunctionsforthispurposeThenarginfunctiondeterminesthenumberofinput argumentsineitherauserdefinedfunctionorabuiltin functionThenameofthefunctionmustbespecifiedasastring,as, forexample:in nargin(sin)Continued….Whennarginisusedinsideauserdefinedfunction,it determineshowmanyinputargumentswereactually enteredThisallowsafunctiontohaveavariablenumberofinputsRecallgraphingfunctionssuchassurfWhensurfhasasinglematrixinput,agraphiscreated, usingthematrixindexnumbersasthex–andy coordinates.Whentherearethreeinputs,x,y,andz, thegraphisbasedonthespecifiedxandy–valuesThenarginfunctionallowstheprogrammertodetermineContinued….Thesurffunctionisanexampleofafunctionwitha variablenumberofinputsIfweusenarginfromthecommandwindowtodetermine thenumberofdeclaredinputs,thereisn’tonecorrect answerThenarginfunctionreturnsanegativenumbertoletus knowthatavariablenumberofinputsarepossible: nargin(surf) ans=1Thenargoutfunctionissimilartonargin,butit determinesthenumberofoutputsfromafunction:Continued….Anexampleofafunctionwithmultipleoutputsismax: nargout(max) ans=2Whenusedinsideauserdefinedfunction,nargout determineshowmanyoutputshavebeenrequestedbythe userConsiderthisexample,inwhichwehaverewrittenthe functiontocreateastar: functionA=star1() theta=pi/2:0.8*pi:4.8*pi; r=ones(1,6);LocalVariablesThevariablesusedinfunctionMfilesareknownaslocal variablesTheonlywayafunctioncancommunicatewiththe workspaceisthroughinputargumentsandtheoutputit returnsAnyvariablesdefinedwithinthefunctionexistonlyfor thefunctiontouseForexample:considerthegfunctionpreviously described: functionoutput=g(x,y) %ThisfunctionmultipliesxandytogetherContinued….Justascalculationsperformedinthecommandwindowor fromascriptMfilecannotaccessvariablesdefinedin functions,functionscannotaccessthevariablesdefinedin theworkspaceThismeansthatfunctionsmustbecompletelyself contained:Theonlywaytheycangetinformationfrom yourprogramisthroughtheinputarguments,andtheonly waytheycandeliverinformationisthroughthefunction outputConsiderafunctionwrittentofindthedistanceanobject fallsduetogravity: functionresult=distance(t)Continued….ThevalueofgmustbeincludedinsidethefunctionItdoesn’tmatterwhetherghasorhasnotbeenusedinthe mainprogramHowgisdefinedishiddentothedistancefunctionunless gisspecifiedinsidethefunctionOfcourse,youcouldalsopassthevalueofgtothe functionasaninputargument: functionresult=distance(g,t) %Thisfunctioncalculatesthedistanceafallingobject GlobalVariablesUnlikelocalvariables,globalvariablesareavailabletoall partsofacomputerprogramIngeneral,itisabadideatodefineglobalvariablesHowever,MATLABprotectsusersfromunintentionally usingaglobalvariablebyrequiringthatitbeidentified bothinthecommandwindowenvironmentandinthe functionthatwilluseitConsiderthedistancefunctiononceagain: functionresult=distance(t) %ThisfunctioncalculatesthedistanceafallingobjectCreatingToolBoxofFunctionsWhenafunctioniscalledinMATLAB,theprogramfirst looksinthecurrentfoldertoseeifthefunctionisdefinedIfitcan’tfindthefunctionlistedthere,itstartsdowna predefinedsearchpath,lookingforafilewiththe functionnameToviewthepaththeprogramtakesasitlooksforfiles, select File>SetPathfromthemenubarortype pathtoolContinued….Asmoreandmorefunctionsarecreatedtousein programming,itmaybeneededtomodifythepathtolook inadirectorywherepersonaltoolshavebeenstored.Forexample:supposeyouhavestoredthedegreesto radiansandradianstodegreesfunctionscreatedina directorycalledMy_functions.Youcanaddthisdirectory tothepathbyselectingAddFolderfromthelistofoption buttonsintheSetPathdialogwindow.You’llbe promptedtoeithersupplythefolderlocationorbrowseto findit(showninnextslide)Continued….MATLAB nowfirst looksinto thecurrent folderfor function definitions andthen works downthe modified searchpathContinued….Onceafolderisaddedtothepath,thechangeappliesonly tothecurrentMATLABsession,unlesschangesaresaved permanentlyClearly,permanentchangesshouldnevermaketoa publiccomputerHowever,ifsomeoneel ...

Tài liệu được xem nhiều: