Using MATLAB (779505), страница 100
Текст из файла (страница 100)
Adirectory may contain many functions (M-files). Function names are uniqueonly within a single directory (e.g., more than one directory many contain afunction called pie3). When you type a function name on the command line,MATLAB must search all the directories it is aware of to determine whichfunction to call. This list of directories is called the MATLAB path.When looking for a function, MATLAB searches the directories in the orderthey are listed in the path, and calls the first function whose name matches thename of the specified function.If you write an M-file called pie3.m and put it in a directory that is searchedbefore the specgraph directory that contains MATLAB’s pie3 function, thenMATLAB uses your pie3 function instead (note that this is not true for built-infunctions like plot, which are always found first).Object-oriented programming allows you to have many methods (MATLABfunctions located in class directories) with the same name and enablesMATLAB to determine which method to use based on the type or class of thevariables passed to the function.
For example, if p is a portfolio object, thenpie3(p)calls @portfolio/pie3.m because the argument is a portfolio object.Selecting a MethodWhen you call a method for which there are multiple versions with the samename, MATLAB determines the method to call by:• Looking at the classes of the objects in the argument list to determine whichargument has the highest object precedence; the class of this object controlsthe method selection and is called the dispatch type.• Applying the function precedence order to determine which of possiblyseveral implementations of a method to call. This order is determined by thelocation and type of function.22-6722MATLAB Classes and ObjectsDetermining the Dispatch TypeMATLAB first determines which argument controls the method selection.
Theclass type of this argument then determines the class in which MATLABsearches for the method. The controlling argument is either:• The argument with the highest precedence, or• The leftmost of arguments having equal precedenceUser-defined objects take precedence over MATLAB’s built-in classes such asdouble or char. You can set the relative precedence of user-defined objects withthe inferiorto and superiorto functions, as described in “Object Precedence”on page 22-65.MATLAB searches for functions by name. When you call a function, MATLABknows the name, number of arguments, and the type of each argument.MATLAB uses the dispatch type to choose among multiple functions of thesame name, but does not consider the number of arguments.Function Precedence OrderThe function precedence order determines the precedence of one function overanother based on the type of function and its location on the MATLAB path.From the perspective of method selection, MATLAB contains two types offunctions: those built into MATLAB, and those written as M-files.
MATLABtreats these types differently when determining the function precedence order.MATLAB selects the correct function for a given context by applying thefollowing function precedence rules, in the order given.For built-in functions:1 Overloaded MethodsIf there is a method in the class directory of the dispatching argument thathas the same name as a MATLAB built-in function, then this method iscalled instead of the built-in function.2 Nonoverloaded MATLAB FunctionsIf there is no overloaded method, then the MATLAB built-in function iscalled.22-68How MATLAB Determines Which Method to CallMATLAB built-in functions take precedence over both subfunctions andprivate functions.
Therefore, subfunctions or private functions with the samename as MATLAB built-in functions can never be called.For nonbuilt-in functions:1 SubfunctionsSubfunctions take precedence over all other M-file functions and overloadedmethods that are on the path and have the same name. Even if the functionis called with an argument of type matching that of an overloaded method,MATLAB uses the subfunction and ignores the overloaded method.2 Private FunctionsPrivate functions are called if there is no subfunction of the same namewithin the current scope.
As with subfunctions, even if the function is calledwith an argument of type matching that of an overloaded method, MATLABuses the private function and ignores the overloaded method.3 Class Constructor FunctionsConstructor functions (functions having names that are the same as the @directory, for example @polynom/polynom.m) take precedence over otherMATLAB functions. Therefore, if you create an M-file called polynom.m andput it on your path before the constructor @polynom/polynom.m version,MATLAB will always call the constructor version.4 Overloaded MethodsMATLAB calls an overloaded method if it is not masked by a subfunction orprivate function.5 Current DirectoryA function in the current working directory is selected before one elsewhereon the path.6 Elsewhere On PathFinally, a function anywhere else on the path is selected.22-6922MATLAB Classes and ObjectsSelecting Methods from Multiple DirectoriesThere may be a number of directories on the path that contain methods withthe same name.
MATLAB stops searching when it finds the firstimplementation of the method on the path, regardless of the implementationtype (MEX-file, P-code, M-file).Selecting Methods from Multiple Implementation TypesThere are four file precedence types. MATLAB uses file precedence to selectbetween identically named functions in the same directory. The order ofprecedence for file types is:1 MEX-files2 MDL-file (Simulink model)3 P-code4 M-fileFor example, if MATLAB finds a P-code and an M-file version of a method in aclass directory, then the P-code version is used. It is, therefore, important toregenerate the P-code version whenever you edit the M-file.Querying Which Method MATLAB Will CallYou can determine which method MATLAB will call using the which command.For example,which pie3your_matlab_path/toolbox/matlab/specgraph/pie3.mHowever, if p is a portfolio object,which pie3(p)dir_on_your_path/@portfolio/pie3.m % portfolio methodThe which command determines which version of pie3 MATLAB will call if youpassed a portfolio object as the input argument.
To see a list of all versions of aparticular function that are on your MATLAB path, use the −all option. Seethe which reference page for more information on this command.22-70AExternal Interfaces andthe MATLAB APIFinding the Documentation in Online Help . . . . . . A-2Reference Documentation . . . . . . . . . . . .
. . A-4AExternal Interfaces and the MATLAB APIFinding the Documentation in Online HelpMATLAB provides interface capabilities that allow you to communicatebetween MATLAB and the following programs and devices:• External C and Fortran programs• Object-oriented technologies like Java and ActiveX• Hardware devices on your computer’s serial portYou can also import and export data to and from MATLAB.These interfaces, also referred to as the MATLAB Application ProgramInterface (API), are documented in full in the online help. Use the followingpath to locate the help sections listed below.MATLAB -> Using MATLAB -> External Interfaces/APICalling C and Fortran Programs from MATLABMATLAB provides an interface to external programs written in the C andFortran languages that enables you to interact with data and programsexternal to the MATLAB environment.
This section explains how to call yourown C or Fortran subroutines from MATLAB as if they were built-in functions.Creating C Language MEX-FilesMATLAB callable C and Fortran programs are referred to as MEX-files. Thissection explains how to create and work with C MEX-files.Creating Fortran MEX-FilesThis section explains how to create and work with Fortran MEX-files.Calling MATLAB from C and Fortran ProgramsYou can employ MATLAB as a computational engine that responds to callsfrom your C and Fortran programs.
This section describes the MATLABfunctions that allow you to:• Start and end a MATLAB process• Send commands to and exchange data with MATLAB• Compile and link MATLAB engine programsA-2Calling Java from MATLABThis section describes how to use the MATLAB interface to Java classes andobjects. This MATLAB capability enables you to:• Bring Java classes into the MATLAB environment• Construct objects from those classes• Work with Java arrays in MATLAB• Call methods on Java objects, passing MATLAB or Java data typesImporting and Exporting DataThis section describes how to use MAT-files to import data to and export datafrom the MATLAB environment.
MAT-files provide a convenient mechanismfor moving your MATLAB data between different platforms in a highlyportable manner. In addition, they provide a means to import and export yourdata to other stand-alone MATLAB applications.ActiveX and DDE SupportMATLAB has interfaces that allow you to interact with ActiveX and DynamicData Exchange, (DDE). This section explains how to:• Integrate ActiveX control components into an ActiveX control container suchas a MATLAB figure window• Use ActiveX Automation components to control or be controlled by MATLAB• Enable access between MATLAB and other Windows applications usingDDESerial Port I/OThis section describes the MATLAB serial port interface which provides directaccess to peripheral devices such as modems, printers, and scientificinstruments that you connect to your computer’s serial port.
This interface isestablished through a serial port object that allows you to:• Configure serial port communications• Use serial port control pins• Write data to and read data from the device• Execute an action when a particular event occurs• Create a record of your serial port sessionA-3AExternal Interfaces and the MATLAB APIReference DocumentationThe online Help Reference section provides a detailed description of each of theMATLAB functions available in the MATLAB external interfaces.
You can findthis documentation in the online Help Contents by following the path shownhere. Click on the page called External Interfaces/API Reference to seelinks to the sections listed below.MATLAB -> Reference -> External Interfaces/API ReferenceAPI NotesAn introduction to using the mex script, the MATLAB mxArray and other datatypes, and passing pointers in Fortran.C Engine RoutinesFunctions that allow you to call MATLAB from your own C programs.C MAT-File RoutinesFunctions that allow you to incorporate and use MATLAB data in your own Cprograms.C MEX-FunctionsFunctions that you use in your C files to perform operations in the MATLABenvironment.C MX-FunctionsArray access and creation functions that you use in your C files to manipulateMATLAB arrays.Fortran Engine RoutinesFunctions that allow you to call MATLAB from your own Fortran programs.Fortran MAT-File RoutinesFunctions that allow you to incorporate and use MATLAB data in your ownFortran programs.Fortran MEX-FunctionsFunctions that you use in your Fortran files to perform operations in theMATLAB environment.A-4Reference DocumentationFortran MX-FunctionsArray access and creation functions that you use in your Fortran files tomanipulate MATLAB arrays.Java Interface FunctionsFunctions that enable you to create and interact with Java classes and objectsfrom MATLAB.ActiveX FunctionsFunctions that create ActiveX objects and manipulate their interfaces.DDE FunctionsDynamic Data Exchange functions that enable MATLAB to access otherWindows applications and vice versa.Serial Port I/O FunctionsFunctions that enable you to interact with devices connected to yourcomputer’s serial port.A-5AExternal Interfaces and the MATLAB APIA-6IndexSymbols! function 3-12%for comment lines 17-11for H1 and help text lines 17-10& 17-29...















