MATLAB Programming Fundamentals

2y ago
31 Views
7 Downloads
5.78 MB
1.1K Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Vicente Bone
Transcription

MATLAB Programming FundamentalsR2015b

How to Contact MathWorksLatest news:www.mathworks.comSales and services:www.mathworks.com/sales and servicesUser community:www.mathworks.com/matlabcentralTechnical support:www.mathworks.com/support/contact usPhone:508-647-7000The MathWorks, Inc.3 Apple Hill DriveNatick, MA 01760-2098MATLAB Programming Fundamentals COPYRIGHT 1984–2015 by The MathWorks, Inc.The software described in this document is furnished under a license agreement. The software may be usedor copied only under the terms of the license agreement. No part of this manual may be photocopied orreproduced in any form without prior written consent from The MathWorks, Inc.FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentationby, for, or through the federal government of the United States. By accepting delivery of the Programor Documentation, the government hereby agrees that this software or documentation qualifies ascommercial computer software or commercial computer software documentation as such terms are usedor defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms andconditions of this Agreement and only those rights specified in this Agreement, shall pertain to andgovern the use, modification, reproduction, release, performance, display, and disclosure of the Programand Documentation by the federal government (or other entity acquiring for or through the federalgovernment) and shall supersede any conflicting contractual terms or conditions. If this License failsto meet the government's needs or is inconsistent in any respect with federal procurement law, thegovernment agrees to return the Program and Documentation, unused, to The MathWorks, Inc.TrademarksMATLAB and Simulink are registered trademarks of The MathWorks, Inc. Seewww.mathworks.com/trademarks for a list of additional trademarks. Other product or brandnames may be trademarks or registered trademarks of their respective holders.PatentsMathWorks products are protected by one or more U.S. patents. Please seewww.mathworks.com/patents for more information.

Revision HistoryJune 2004October 2004March 2005June 2005September 2005March 2006September 2006March 2007September 2007March 2008October 2008March 2009September 2009March 2010September 2010April 2011September 2011March 2012September 2012March 2013September 2013March 2014October 2014March 2015September 2015First printingOnline onlyOnline onlySecond printingOnline onlyOnline onlyOnline onlyOnline onlyOnline onlyOnline onlyOnline onlyOnline onlyOnline onlyOnline onlyOnline onlyOnline onlyOnline onlyOnline onlyOnline onlyOnline onlyOnline onlyOnline onlyOnline onlyOnline onlyOnline onlyNew for MATLAB 7.0 (Release 14)Revised for MATLAB 7.0.1 (Release 14SP1)Revised for MATLAB 7.0.4 (Release 14SP2)Minor revision for MATLAB 7.0.4Revised for MATLAB 7.1 (Release 14SP3)Revised for MATLAB 7.2 (Release 2006a)Revised for MATLAB 7.3 (Release 2006b)Revised for MATLAB 7.4 (Release 2007a)Revised for Version 7.5 (Release 2007b)Revised for Version 7.6 (Release 2008a)Revised for Version 7.7 (Release 2008b)Revised for Version 7.8 (Release 2009a)Revised for Version 7.9 (Release 2009b)Revised for Version 7.10 (Release 2010a)Revised for Version 7.11 (Release 2010b)Revised for Version 7.12 (Release 2011a)Revised for Version 7.13 (Release 2011b)Revised for Version 7.14 (Release 2012a)Revised for Version 8.0 (Release 2012b)Revised for Version 8.1 (Release 2013a)Revised for Version 8.2 (Release 2013b)Revised for Version 8.3 (Release 2014a)Revised for Version 8.4 (Release 2014b)Revised for Version 8.5 (Release 2015a)Revised for Version 8.6 (Release 2015b)

ContentsLanguage1Syntax BasicsCreate Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-2Create Numeric Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . .1-3Continue Long Statements on Multiple Lines . . . . . . . .1-5Call Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-6Ignore Function Outputs . . . . . . . . . . . . . . . . . . . . . . . . .1-7Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Valid Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Conflicts with Function Names . . . . . . . . . . . . . . . . . . .1-81-81-8Case and Space Sensitivity . . . . . . . . . . . . . . . . . . . . . . .1-10Command vs. Function Syntax . . . . . . . . . . . . . . . . . . .Command and Function Syntaxes . . . . . . . . . . . . . . . .Avoid Common Syntax Mistakes . . . . . . . . . . . . . . . . .How MATLAB Recognizes Command Syntax . . . . . . .1-121-121-131-14Common Errors When Calling Functions . . . . . . . . . . .Conflicting Function and Variable Names . . . . . . . . . .Undefined Functions or Variables . . . . . . . . . . . . . . . .1-161-161-16v

2viContentsProgram ComponentsArray vs. Matrix Operations . . . . . . . . . . . . . . . . . . . . . .Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Array Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-22-22-22-4Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Relational Operators and Arrays . . . . . . . . . . . . . . . . . .Relational Operators and Empty Arrays . . . . . . . . . . . .2-72-72-7Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Precedence of AND and OR Operators . . . . . . . . . . . . .Overriding Default Precedence . . . . . . . . . . . . . . . . . . .2-92-92-9Average Similar Data Points Using a Tolerance . . . . .2-11Group Scattered Data Using a Tolerance . . . . . . . . . . .2-14Special Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-17Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . .2-19Loop Control Statements . . . . . . . . . . . . . . . . . . . . . . . .2-21Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . .What Is a Regular Expression? . . . . . . . . . . . . . . . . . .Steps for Building Expressions . . . . . . . . . . . . . . . . . .Operators and Characters . . . . . . . . . . . . . . . . . . . . . .2-232-232-252-28Lookahead Assertions in Regular Expressions . . . . . .Lookahead Assertions . . . . . . . . . . . . . . . . . . . . . . . . .Overlapping Matches . . . . . . . . . . . . . . . . . . . . . . . . . .Logical AND Conditions . . . . . . . . . . . . . . . . . . . . . . .2-392-392-392-40Tokens in Regular Expressions . . . . . . . . . . . . . . . . . . .Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Multiple Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Unmatched Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . .Tokens in Replacement Strings . . . . . . . . . . . . . . . . . .Named Capture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-422-422-432-442-452-45

Dynamic Regular Expressions . . . . . . . . . . . . . . . . . . . .Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Dynamic Match Expressions — (?expr) . . . . . . . . . . .Commands That Modify the Match Expression — (?@cmd) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Commands That Serve a Functional Purpose — (?@cmd) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Commands in Replacement Expressions — {cmd} . . .2-512-53Comma-Separated Lists . . . . . . . . . . . . . . . . . . . . . . . . .What Is a Comma-Separated List? . . . . . . . . . . . . . . .Generating a Comma-Separated List . . . . . . . . . . . . .Assigning Output from a Comma-Separated List . . . . .Assigning to a Comma-Separated List . . . . . . . . . . . . .How to Use the Comma-Separated Lists . . . . . . . . . . .Fast Fourier Transform Example . . . . . . . . . . . . . . . .2-562-562-562-582-592-612-63Alternatives to the eval Function . . . . . . . . . . . . . . . . .Why Avoid the eval Function? . . . . . . . . . . . . . . . . . . .Variables with Sequential Names . . . . . . . . . . . . . . . .Files with Sequential Names . . . . . . . . . . . . . . . . . . . .Function Names in Variables . . . . . . . . . . . . . . . . . . .Field Names in Variables . . . . . . . . . . . . . . . . . . . . . .Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-652-652-652-662-672-672-68Symbol Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Asterisk — * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .At — @ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Colon — : . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Comma — , . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Curly Braces — { } . . . . . . . . . . . . . . . . . . . . . . . . . . .Dot — . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Dot-Dot — . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Dot-Dot-Dot (Ellipsis) — . . . . . . . . . . . . . . . . . . . . . .Dot-Parentheses — .( ) . . . . . . . . . . . . . . . . . . . . . . . . .Exclamation Point — ! . . . . . . . . . . . . . . . . . . . . . . . .Parentheses — ( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . .Percent — % . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Percent-Brace — %{ %} . . . . . . . . . . . . . . . . . . . . . . . .Plus — . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Semicolon — ; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Single Quotes — ' ' . . . . . . . . . . . . . . . . . . . . . . . . . . .Space Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Slash and Backslash — / \ . . . . . . . . . . . . . . . . . . . . -762-762-772-772-782-782-792-482-482-492-50vii

Square Brackets — [ ] . . . . . . . . . . . . . . . . . . . . . . . . .Tilde — . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-792-80Classes (Data Types)3Overview of MATLAB ClassesFundamental MATLAB Classes . . . . . . . . . . . . . . . . . . . .4viiiContents3-2Numeric ClassesIntegers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Integer Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating Integer Data . . . . . . . . . . . . . . . . . . . . . . . . . .Arithmetic Operations on Integer Classes . . . . . . . . . . .Largest and Smallest Values for Integer Classes . . . . . .Integer Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-24-24-34-44-54-5Floating-Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . .Double-Precision Floating Point . . . . . . . . . . . . . . . . . .Single-Precision Floating Point . . . . . . . . . . . . . . . . . . .Creating Floating-Point Data . . . . . . . . . . . . . . . . . . . .Arithmetic Operations on Floating-Point Numbers . . . .Largest and Smallest Values for Floating-Point ClassesAccuracy of Floating-Point Data . . . . . . . . . . . . . . . . .Avoiding Common Problems with Floating-PointArithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Floating-Point Functions . . . . . . . . . . . . . . . . . . . . . . .References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-64-64-64-74-84-94-11Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating Complex Numbers . . . . . . . . . . . . . . . . . . . .Complex Number Functions . . . . . . . . . . . . . . . . . . . .4-164-164-174-124-144-14

56Infinity and NaN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Infinity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .NaN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Infinity and NaN Functions . . . . . . . . . . . . . . . . . . . .4-184-184-184-20Identifying Numeric Classes . . . . . . . . . . . . . . . . . . . . .4-21Display Format for Numeric Values . . . . . . . . . . . . . . .Default Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Display Format Examples . . . . . . . . . . . . . . . . . . . . . .Setting Numeric Format in a Program . . . . . . . . . . . .4-224-224-224-23Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-25The Logical ClassFind Array Elements That Meet a Condition . . . . . . . . .Apply a Single Condition . . . . . . . . . . . . . . . . . . . . . . .Apply Multiple Conditions . . . . . . . . . . . . . . . . . . . . . .Replace Values that Meet a Condition . . . . . . . . . . . . . .5-25-25-45-5Determine if Arrays Are Logical . . . . . . . . . . . . . . . . . . .Identify Logical Matrix . . . . . . . . . . . . . . . . . . . . . . . . .Test an Entire Array . . . . . . . . . . . . . . . . . . . . . . . . . . .Test Each Array Element . . . . . . . . . . . . . . . . . . . . . . .Summary Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-75-75-75-85-9Reduce Logical Arrays to Single Value . . . . . . . . . . . . .5-10Truth Table for Logical Operations . . . . . . . . . . . . . . .5-13Characters and StringsCreating Character Arrays . . . . . . . . . . . . . . . . . . . . . . . .Creating a Character String . . . . . . . . . . . . . . . . . . . . .6-26-2ix

xContentsCreating a Rectangular Character Array . . . . . . . . . . . .Identifying Characters in a String . . . . . . . . . . . . . . . .Working with Space Characters . . . . . . . . . . . . . . . . . .Expanding Character Arrays . . . . . . . . . . . . . . . . . . . . .6-36-46-56-6Cell Arrays of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . .Converting to a Cell Array of Strings . . . . . . . . . . . . . .Functions for Cell Arrays of Strings . . . . . . . . . . . . . . .6-76-76-8Formatting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Functions that Use Format Strings . . . . . . . . . . . . . . .The Format String . . . . . . . . . . . . . . . . . . . . . . . . . . .Input Value Arguments . . . . . . . . . . . . . . . . . . . . . . . .The Formatting Operator . . . . . . . . . . . . . . . . . . . . . .Constructing the Formatting Operator . . . . . . . . . . . .Setting Field Width and Precision . . . . . . . . . . . . . . . .Restrictions for Using Identifiers . . . . . . . . . . . . . . . .6-106-106-116-126-136-146-196-21String Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Comparing Strings for Equality . . . . . . . . . . . . . . . . .Comparing for Equality Using Operators . . . . . . . . . .Categorizing Characters Within a String . . . . . . . . . . .6-236-236-246-24Searching and Replacing . . . . . . . . . . . . . . . . . . . . . . . .6-26Converting from Numeric to String . . . . . . . . . . . . . . .Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . .Converting to a Character Equivalent . . . . . . . . . . . . .Converting to a String of Numbers . . . . . . . . . . . . . . .Converting to a Specific Radix . . . . . . . . . . . . . . . . . .6-286-286-296-296-29Converting from String to Numeric . . . . . . . . . . . . . . .Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . .Converting from a Character Equivalent . . . . . . . . . . .Converting from a Numeric String . . . . . . . . . . . . . . .Converting from a Specific Radix . . . . . . . . . . . . . . . .6-306-306-306-316-31Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-33

7Dates and TimeRepresent Dates and Times in MATLAB . . . . . . . . . . . .7-2Specify Time Zones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-6Set Date and Time Display Format . . . . . . . . . . . . . . . . .Formats for Individual Date and Duration Arrays . . . . .datetime Display Format . . . . . . . . . . . . . . . . . . . . . .duration Display Format . . . . . . . . . . . . . . . . . . . . . .calendarDuration Display Format . . . . . . . . . . . . .Default datetime Format . . . . . . . . . . . . . . . . . . . . .7-87-87-87-97-107-11Generate Sequence of Dates and Time . . . . . . . . . . . . .Sequence of Datetime or Duration Values BetweenEndpoints with Step Size . . . . . . . . . . . . . . . . . . . .Add Duration or Calendar Duration to Create Sequence ofDates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Specify Length and Endpoints of Date or DurationSequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Sequence of Datetime Values Using Calendar Rules . .7-137-177-18Share Code and Data Across Locales . . . . . . . . . . . . . .Write Locale-Independent Date and Time Code . . . . . .Write Dates in Other Languages . . . . . . . . . . . . . . . . .Read Dates in Other Languages . . . . . . . . . . . . . . . . .7-227-227-237-24Extract or Assign Date and Time Components ofDatetime Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-25Combine Date and Time from Separate Variables . . . .7-30Date and Time Arithmetic . . . . . . . . . . . . . . . . . . . . . . .7-32Compare Dates and Time . . . . . . . . . . . . . . . . . . . . . . . .7-40Plot Dates and Durations . . . . . . . . . . . . . . . . . . . . . . . .7-44Core Functions Supporting Date and Time Arrays . . .7-557-137-16xi

8xiiContentsConvert Between Datetime Arrays, Numbers, andStrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Convert Between Datetime and Strings . . . . . . . . . . .Convert Between Datetime and Date Vectors . . . . . . .Convert Serial Date Numbers to Datetime . . . . . . . . .Convert Datetime Arrays to Numeric Values . . . . . . . .7-567-567-577-587-597-59Carryover in Date Vectors and Strings . . . . . . . . . . . . .7-61Converting Date Vector Returns Unexpected Output .7-62Categorical ArraysCreate Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . .8-2Convert Table Variables Containing Strings toCategorical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-6Plot Categorical Data . . . . . . . . . . . . . . . . . . . . . . . . . . .8-11Compare Categorical Array Elements . . . . . . . . . . . . . .8-19Combine Categorical Arrays . . . . . . . . . . . . . . . . . . . . .8-22Combine Categorical Arrays Using Multiplication . . .8-26Access Data Using Categorical Arrays . . . . . . . . . . . . .Select Data By Category . . . . . . . . . . . . . . . . . . . . . . .Common Ways to Access Data Using CategoricalArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-298-29Work with Protected Categorical Arrays . . . . . . . . . . .8-37Advantages of Using Categorical Arrays . . . . . . . . . . .Natural Representation of Categorical Data . . . . . . . .Mathematical Ordering for Strings . . . . . . . . . . . . . . .Reduce Memory Requirements . . . . . . . . . . . . . . . . . .8-428-428-428-428-29

Ordinal Categorical Arrays . . . . . . . . . . . . . . . . . . . . . .Order of Categories . . . . . . . . . . . . . . . . . . . . . . . . . . .How to Create Ordinal Categorical Arrays . . . . . . . . .Working with Ordinal Categorical Arrays . . . . . . . . . .8-458-458-458-48Core Functions Supporting Categorical Arrays . . . . . .8-49Tables9Create a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-2Add and Delete Table Rows . . . . . . . . . . . . . . . . . . . . . . .9-9Add and Delete Table Variables . . . . . . . . . . . . . . . . . .9-13Clean Messy and Missing Data in Tables . . . . . . . . . . .9-17Modify Units, Descriptions and Table Variable Names9-24Access Data in a Table . . . . . . . . . . . . . . . . . . . . . . . . . .Ways to Index into a Table . . . . . . . . . . . . . . . . . . . . .Create Table from Subset of Larger Table . . . . . . . . . .Create Array from the Contents of Table . . . . . . . . . . .9-289-289-299-32Calculations on Tables . . . . . . . . . . . . . . . . . . . . . . . . . .9-36Split Data into Groups and Calculate Statistics . . . . .9-40Split Table Data Variables and Apply Functions . . . . .9-44Advantages of Using Tables . . . . . . . . . . . . . . . . . . . . . .Conveniently Store Mixed-Type Data in SingleContainer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Access Data Using Numeric or Named Indexing . . . . .Use Table Properties to Store Metadata . . . . . . . . . . .9-49Grouping Variables To Split Data . . . . . . . . . . . . . . . . .Grouping Variables . . . . . . . . . . . . . . . . . . . . . . . . . . .Group Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-569-569-569-499-529-53xiii

The Split-Apply-Combine Workflow . . . . . . . . . . . . . . .Missing Group Values . . . . . . . . . . . . . . . . . . . . . . . . .1011xivContents9-579-58StructuresCreate a Structure Array . . . . . . . . . . . . . . . . . . . . . . . .10-2Access Data in a Structure Array . . . . . . . . . . . . . . . . .10-6Concatenate Structures . . . . . . . . . . . . . . . . . . . . . . . .10-10Generate Field Names from Variables . . . . . . . . . . . .10-12Access Data in Nested Structures . . . . . . . . . . . . . . . .10-13Access Elements of a Nonscalar Struct Array . . . . . .10-15Ways to Organize Data in Structure Arrays . . . . . . . .Plane Organization . . . . . . . . . . . . . . . . . . . . . . . . . .Element-by-Element Organization . . . . . . . . . . . . . . .10-1710-1710-19Memory Requirements for a Structure Array . . . . . .10-21Cell ArraysWhat Is a Cell Array? . . . . . . . . . . . . . . . . . . . . . . . . . . .11-2Create a Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-3Access Data in a Cell Array . . . . . . . . . . . . . . . . . . . . . .11-5Add Cells to a Cell Array . . . . . . . . . . . . . . . . . . . . . . . .11-8Delete Data from a Cell Array . . . . . . . . . . . . . . . . . . . .11-9

1213Combine Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . .11-10Pass Contents of Cell Arrays to Functions . . . . . . . . .11-11Preallocate Memory for a Cell Array . . . . . . . . . . . . .11-16Cell vs. Struct Arrays . . . . . . . . . . . . . . . . . . . . . . . . . .11-17Multilevel Indexing to Access Parts of Cells . . . . . . .11-19Function HandlesCreate Function Handle . . . . . . . . . . . . . . . . . . . . . . . . .What Is a Function Handle? . . . . . . . . . . . . . . . . . . . .Creating Function Handles . . . . . . . . . . . . . . . . . . . . .Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . .Arrays of Function Handles . . . . . . . . . . . . . . . . . . . .Saving and Loading Function Handles . . . . . . . . . . . .12-212-212-212-412-412-5Pass Function to Another Function . . . . . . . . . . . . . . .12-6Call Local Functions Using Function Handles . . . . . . .12-8Compare Function Handles . . . . . . . . . . . . . . . . . . . . .Compare Handles Constructed from Named FunctionCompare Handles to Anonymous Functions . . . . . . . .Compare Handles to Nested Functions . . . . . . . . . . .Call Local Functions Using Function Handles . . . . . .12-1012-1012-1012-1112-12Map ContainersOverview of the Map Data Structure . . . . . . . . . . . . . .13-2Description of the Map Class . . . . . . . . . . . . . . . . . . . . .Properties of the Map Class . . . . . . . . . . . . . . . . . . . .13-413-4xv

Methods of the Map Class . . . . . . . . . . . . . . . . . . . . . .14xviContents13-5Creating a Map Object . . . . . . . . . . . . . . . . . . . . . . . . . .Constructing an Empty Map Object . . . . . . . . . . . . . .Constructing An Initialized Map Object . . . . . . . . . . .Combining Map Objects . . . . . . . . . . . . . . . . . . . . . . .13-613-613-713-8Examining the Contents of the Map . . . . . . . . . . . . . . .13-9Reading and Writing Using a Key Index . . . . . . . . . . .Reading From the Map . . . . . . . . . . . . . . . . . . . . . . .Adding Key/Value Pairs . . . . . . . . . . . . . . . . . . . . . .Building a Map with Concatenation . . . . . . . . . . . . .13-1013-1013-1113-12Modifying Keys and Values in the Map . . . . . . . . . . . .Removing Keys and Values from the Map . . . . . . . . .Modifying Values . . . . . . . . . . . . . . . . . . . . . . . . . . . .Modifying Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Modifying a Copy of the Map . . . . . . . . . . . . . . . . . .13-1513-1513-1613-1613-17Mapping to Different Value Types . . . . . . . . . . . . . . . .Mapping to a Structure Array . . . . . . . . . . . . . . . . . .Mapping to a Cell Array . . . . . . . . . . . . . . . . . . . . . .13-1813-1813-19Combining Unlike ClassesValid Combinations of Unlike Classes . . . . . . . . . . . . .14-2Combining Unlike Integer Types . . . . . . . . . . . . . . . . . .Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Example of Combining Unlike Integer Sizes . . . . . . . .Example of Combining Signed with Unsigned . . . . . . .14-314-314-314-4Combining Integer and Noninteger Data . . . . . . . . . . .14-6Combining Cell Arrays with Non-Cell Arrays . . . . . . .14-7Empty Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14-8

Concatenation Examples . . . . . . . . . . . . . . . . . . . . . . . .Combining Single and Double Types . . . . . . . . . . . . . .Combining Integer and Double Types . . . . . . . . . . . . .Combining Character and Double Types . . . . . . . . . .Combining Logical and Double Types . . . . . . . . . . . .1514-914-914-914-1014-10Using ObjectsMATLAB Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Getting Oriented . . . . . . . . . . . . . . . . . . . . . . . . . . . . .What Are Objects and Why Use Them? . . . . . . . . . . . .Working with Objects . . . . . . . . . . . . . . . . . . . . . . . . .Objects In the MATLAB Language . . . . . . . . . . . . . . .Other Kinds of Objects Used by MATLAB . . . . . . . . . .15-215-215-215-315-315-3General Purpose Vs. Specialized Arrays . . . . . . . . . . . .How They Differ . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using General-Purpose Data Structures . . . . . . . . . . .Using Specialized Objects . . . . . . . . . . . . . . . . . . . . . .15-515-515-515-6Key Object Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . .Basic Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Classes Describe How to Create Objects . . . . . . . . . . .Properties Contain Data . . . . . . . . . . . . . . . . . . . . . . .Methods Implement Operations . . . . . . . . . . . . . . . . .Events are Notices Broadcast to Listening Objects . .15-815-815-815-915-915-10Creating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Class Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . .When to Use Package Names . . . . . . . . . . . . . . . . . .15-1115-1115-11Accessing Object Data . . . . . . . . . . . . . . . . . . . . . . . . . .Listing Public Properties . . . . . . . . . . . . . . . . . . . . . .Getting Property Values . . . . . . . . . . . . . . . . . . . . . .Setting Property Values . . . . . . . . . . . . . . . . . . . . . .15-1315-1315-1315-14Calling Object Methods . . . . . . . . . . . . . . . . . . . . . . . . .What Operations Can You Perform . . . . . . . . . . . . . .Method Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15-1515-1515-15xvii

Class of Objects Returned by Methods . . . . . . . . . . . .15-16Desktop Tools Are Object Aware . . . . . . . . . . . . . . . . .Tab Completion Works with Objects . . . . . . . . . . . . .Editing Objects with the Variables Editor . . . . . . . . .15-1815-1815-18Getting Information About Objects . . . . . . . . . . . . . . .The Class of Workspace Variables . . . . . . . . . . . . . . .Information About Class Members . . . . . . . . . . . . . .Logical Tests for Objects . . . . . . . . . . . . . . . . . . . . . .Displaying Objects . . . . . . . . . . . . . . . . . . . . . . . . . . .Getting Help for MATLAB Objects . . . . . . . . . . . . . .15-2015-2015-2215-2215-2315-24Copying Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Two Copy Behaviors . . . . . . . . . . . . . . . . . . . . . . . . .Value Object Copy Behavior . . . . . . . . . . . . . . . . . . .Handle Object Copy Behavior . . . . . . . . . . . . . . . . . .Testing for Handle or Value Class . . . . . . . . . . . . . . .15-2515-2515-2515-2615-29Destroying Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Object Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Difference Between clear and delete . . . . . . . . . . . . .15-3115-3115-31Defining Your Own Classes16Scripts and Functions17xviiiContentsScriptsCreate Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17-2Add Comments to Programs . . . . . . . . . . . . . . . . . . . . . .17-4

18Run Code Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Divide Your File into Code Sections . . . . . . . . . . . . . .Evaluate Code Sections . . . . . . . . . . . . . . . . . . . . . . . .Navigate Among Code Sections in a File . . . . . . . . . . .Example of Evaluating Code Sections . . . . . . . . . . . . .Change the Appearance of Code Sections . . . . . . . . .Use Code Sections with Control Statements andFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17-617-617-617-817-817-12Scripts vs. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .17-1617-12Function BasicsCreate Functions in Files . . . . . . . . . . . . . . . . . . . . . . . .18-2Add Help for Your Program . . . . . . . . . . . . . . . . . . . . . .18-5Run Functions in the Editor . . . . . . . . . . . . . . . . . . . . .18-7Base and Function Workspaces . . . . . . . . . . . . . . . . . . .18-9Share Data Between Workspaces . . . .

October 2004 Online only Revised for MATLAB 7.0.1 (Release 14SP1) March 2005 Online only Revised for MATLAB 7.0.4 (Release 14SP2) June 2005 Second printing Minor revision for MATLAB 7.0.4 September 2005 Online only Revised for MATLAB 7.1 (Release 14SP3) March 2006 Online only Revised for MATLAB 7.2 (Release 2006a)

Related Documents:

19 MATLAB Excel Add-in Hadoop MATLAB Compiler Standalone Application MATLAB deployment targets MATLAB Compiler enables sharing MATLAB programs without integration programming MATLAB Compiler SDK provides implementation and platform flexibility for software developers MATLAB Production Server provides the most efficient development path for secure and scalable web and enterprise applications

I. Introduction to Programming Using MATLAB Chapter 1: Introduction to MATLAB 1.1 Getting into MATLAB 1.2 The MATLAB Desktop Environment 1.3 Variables and Assignment Statements 1.4 Expressions 1.5 Characters and Encoding 1.6 Vectors and Matrices Chapter 2: Introduction to MATLAB Programming 2.1 Algorithms 2.2 MATLAB Scripts 2.3 Input and Output

MATLAB tutorial . School of Engineering . Brown University . To prepare for HW1, do sections 1-11.6 – you can do the rest later as needed . 1. What is MATLAB 2. Starting MATLAB 3. Basic MATLAB windows 4. Using the MATLAB command window 5. MATLAB help 6. MATLAB ‘Live Scripts’ (for algebra, plotting, calculus, and solving differential .

MATLAB tutorial . School of Engineering . Brown University . To prepare for HW1, do sections 1-11.6 – you can do the rest later as needed . 1. What is MATLAB 2. Starting MATLAB 3. Basic MATLAB windows 4. Using the MATLAB command window 5. MATLAB help 6. MATLAB ‘Live Scripts’ (for

3. MATLAB script files 4. MATLAB arrays 5. MATLAB two‐dimensional and three‐dimensional plots 6. MATLAB used‐defined functions I 7. MATLAB relational operators, conditional statements, and selection structures I 8. MATLAB relational operators, conditional statements, and selection structures II 9. MATLAB loops 10. Summary

June 2004 First printing New for MATLAB 7.0 (Release 14) October 2004 Online only Revised for MATLAB 7.0.1 (Release 14SP1) March 2005 Online only Revised for MATLAB 7.0.4 (Release 14SP2) June 2005 Second printing Minor revision for MATLAB 7.0.4 September 2005 Online only Revised for MATLAB 7.1 (Release 14SP3)

June 2004 First printing New for MATLAB 7.0 (Release 14) October 2004 Online only Revised for MATLAB 7.0.1 (Release 14SP1) March 2005 Online only Revised for MATLAB 7.0.4 (Release 14SP2) June 2005 Second printing Minor revision for MATLAB 7.0.4 September 2005 Online only Revised for MATLAB 7.1 (Release 14SP3)

in Method B per ASTM D 4255. In-plane shear strength testing was used to determine the shear modulus. The 18-in. by 18-in. non-standard butt-joint Thermo-lag 330 specimens were shear tested in a single shearing frame with one frame edge set within 2-in. of the butt-joint and the other frame edge set approximately 1/2-in. outside the end of the mesh overlap. The 6-in. by 6-in. punching shear .