Using MATLAB (779505), страница 72
Текст из файла (страница 72)
SIAM Conferenceon Applied Linear Algebra, Oct. 1997, p. 29.[4] Gilbert, John R., Cleve Moler, and Robert Schreiber, “Sparse Matrices inMATLAB: Design and Implementation,” SIAM J. Matrix Anal. Appl., Vol. 13,No. 1, January 1992, pp. 333-356.[5] Larimore, S.
I., An Approximate Minimum Degree Column OrderingAlgorithm, MS Thesis, Dept. of Computer and Information Science andEngineering, University of Florida, Gainesville, FL, 1998, available athttp://www.cise.ufl.edu/tech-reports/[6] Lehoucq, R. B., D. C. Sorensen, C. Yang, ARPACK Users’ Guide, SIAM,Philadelphia, 1998.[7] Saad, Yousef, Iterative Methods for Sparse Linear Equations.
PWSPublishing Company, 1996.16-42Programming and Data TypesMATLAB is a high-level language that includes data structures, functions,control flow statements, input/output, and object-oriented capabilities. Thissection presents MATLAB’s programming features and techniques in thefollowing chapters:• M-File Programming – describes language constructs and how to createMATLAB programs (M-files). It covers data types, flow control, arrayindexing, optimizing performance, and other topics.• Character Arrays – covers MATLAB's support for string data, including howto create character arrays and cell arrays of strings, ways to representstrings, how to perform common string operations, and conversion betweenstring and numeric formats.• Multidimensional Arrays – discusses the use of MATLAB arrays havingmore than two dimensions.
It covers array creation, array indexing, dataorganization, and how MATLAB functions operate on these arrays.• Structures and Cell Arrays – describes two MATLAB data types that providehierarchical storage for dissimilar kinds of data. Structures contain differentkinds of data organized by named fields.
Cell arrays consist of cells thatthemselves contain MATLAB arrays.• Function Handles - describes how you use the MATLAB function handle tocapture certain information about a function, including function references,that you can then use to execute the function from anywhere in the MATLABenvironment.• MATLAB Classes and Objects – presents MATLAB’s object-orientedprogramming capabilities. Classes and objects enable you to add new datatypes and new operations to MATLAB. This section also includes examplesof how to implement well-behaved classes in MATLAB.Related InformationThe following sections provide information that is also useful to MATLABprogrammers.
This documentation is available in MATLAB’s online help.• Graphics – describes how to plot vector and matrix data in 2-D and 3-Drepresentations, how to annotate, print, and export plots, and how to usegraphics objects and their figure and axes properties.• Calling C and Fortran Programs from MATLAB - describes how to build Cand Fortran subroutines into callable MEX files.• Calling MATLAB from C and Fortran Programs - discusses how to use theMATLAB engine library to call MATLAB from C and Fortran programs.• Calling Java from MATLAB - describes how to use the MATLAB interface toJava classes and objects.• Importing and Exporting Data - describes techniques for importing data toand exporting data from the MATLAB environment.• ActiveX and DDE Support - describes how to use ActiveX and Dynamic DataExchange (DDE) with MATLAB.• Serial Port I/O - describes how to communicate with peripheral devices suchas modems, printers, and scientific instruments that you connect to yourcomputer’s serial port.17M-File ProgrammingMATLAB Programming: A Quick Start .
. . . . . . . 17-3Scripts. . . . . . . . . . . . . . . . . . . . . . 17-7Functions . . . . . . . . . . . . . . . . . . . . . 17-8Local and Global Variables. . . . . . . . . . . . 17-19Data Types . . . . . . . . . . . . . . . . . . . . 17-22Keywords . . . . . . . . . . . . . .
. . . . . . 17-25Operators . . . . . . . . . . . . . . . . . . . . 17-26Flow Control . . . . . . . . . . . . . . . . . . . 17-35Subfunctions . . . . . . . . . . . . . . . . . . . 17-43Private Functions. . . . . . . . . . . . . . . . 17-45Subscripting and IndexingString Evaluation. . . . . . . . . . . . 17-46. . . . . . . . . . . .
. . . . 17-54Command/Function Duality . . . . . . . . . . . . 17-56Empty Matrices. . . . . . . . . . . . . . . . . 17-57Errors and Warnings . . . . . . . . . . . . . . . 17-59Dates and Times . . . . . . . . . . . . . . . . . 17-62Obtaining User Input . . . . . . . . . . . . . . . 17-69Shell Escape Functions . . . .
. . . . . . . . . . 17-70Optimizing MATLAB Code. . . . . . . . . . . . 17-7117M-File ProgrammingMATLAB provides a full programming language that enables you to write aseries of MATLAB statements into a file and then execute them with a singlecommand. You write your program in an ordinary text file, giving the file aname of filename.m. The term you use for filename becomes the newcommand that MATLAB associates with the program.
The file extension of .mmakes this a MATLAB M-file.This chapter explains the basics of how to write script and function programsin M-files. It covers the following topics:• “MATLAB Programming: A Quick Start”• “Scripts”• “Functions”• “Local and Global Variables”• “Data Types”• “Operators”• “Flow Control”• “Subfunctions”• “Private Functions”• “Subscripting and Indexing”• “String Evaluation”• “Command/Function Duality”• “Empty Matrices”• “Errors and Warnings”• “Dates and Times”• “Obtaining User Input”• “Shell Escape Functions”• “Optimizing MATLAB Code”17-2MATLAB Programming: A Quick StartMATLAB Programming: A Quick StartM-files can be scripts that simply execute a series of MATLAB statements, orthey can be functions that also accept arguments and produce output. Youcreate M-files using a text editor, then use them as you would any otherMATLAB function or command.The process looks like this:1 Create an M-file using a texteditor.function c = myfile(a,b)c = sqrt((a.^2)+(b.^2))2 Call the M-file from thecommand line, or from withinanother M-file.a = 7.5b = 3.342c = myfile(a,b)c =8.2109Kinds of M-FilesThere are two kinds of M-files.Script M-FilesFunction M-Files• Do not accept input arguments orreturn output arguments• Can accept input arguments andreturn output arguments• Operate on data in the workspace• Internal variables are local to thefunction by default• Useful for automating a series ofsteps you need to perform manytimes• Useful for extending theMATLAB language for yourapplication17-317M-File ProgrammingWhat’s in an M-File?This section shows you the basic parts of a function M-file, so you canfamiliarize yourself with MATLAB programming and get started with someexamples.function f = fact(n) % Function definition line% FACT Factorial.% H1 line% FACT(N) returns the factorial of N, H! % Help text% usually denoted by N!% Put simply, FACT(N) is PROD(1:N).f = prod(1:n);% Function bodyThis function has some elements that are common to all MATLAB functions:• A function definition line.
This line defines the function name, and thenumber and order of input and output arguments.• An H1 line. H1 stands for “help 1” line. MATLAB displays the H1 line for afunction when you use lookfor or request help on an entire directory.• Help text. MATLAB displays the help text entry together with the H1 linewhen you request help on a specific function.• The function body. This part of the function contains code that performs theactual computations and assigns values to any output arguments.Refer to “Functions” on page 17-8 for more detail on the parts of a MATLABfunction.Providing Help for Your ProgramsYou can provide user information for the programs you write by including ahelp text section at the beginning of your M-file.
This section starts on the linefollowing the function definition and ends at the first blank line. Each line ofthe help text must begin with a comment (%) character. MATLAB displays thisinformation whenever you typehelp m-file_name17-4MATLAB Programming: A Quick StartYou can also make help entries for an entire directory by creating a file withthe special name Contents.m that resides in the directory. This file mustcontain only comment lines; that is, every line must begin with a percent sign.MATLAB displays the lines in a Contents.m file whenever you typehelp directory_nameIf a directory does not contain a Contents.m file, typing help directory_namedisplays the first help line (the H1 line) for each M-file in the directory.Creating M-Files: Accessing Text EditorsM-files are ordinary text files that you create using a text editor.
MATLABprovides a built-in editor, although you can use any text editor you like.Note To open the editor on the PC, from the File menu, choose New, andthen M-File.Another way to edit an M-file is from the MATLAB command line using theedit function. For example,edit fooopens the editor on the file foo.m. Omitting a filename opens the editor on anuntitled file.You can create the fact function shown on the previous page by opening yourtext editor, entering the lines shown, and saving the text in a file called fact.min your current directory.Once you’ve created this file, here are some things you can do:• List the names of the files in your current directorywhat• List the contents of M-file fact.mtype fact17-517M-File Programming• Call the fact functionfact(5)ans =120Note Save any M-files you create and any MATLAB-supplied M-files thatyou edit in a directory that is not in the MATLAB directory tree.
If you keep yourfiles in the MATLAB directory tree, they may be overwritten when you install anew version of MATLAB. Also note that the locations of files in the MATLAB/toolbox directory tree are loaded and cached in memory at the beginning ofeach MATLAB session to improve performance.
If you do save a new or editedfile in the MATLAB/toolbox directory tree, restart MATLAB or use the rehashfunction to reload the directory and update the cache before you use the file.17-6ScriptsScriptsScripts are the simplest kind of M-file because they have no input or outputarguments.















