Sie sind auf Seite 1von 34

Table of Contents

MLBE912V1

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-1

Table of Contents

Objetivo
El objetivo general del curso es dotar a los asistentes de la
habilidad bsica del uso de la herramienta MATLAB, como un
software de computo tcnico. Al final el curso, los asistentes
podrn realizar las 3 tareas bsicas del computo tcnico:
Obtener datos de diversos medios; Procesarlos matemticamente
y visualizarlos; Compartir los resultados a travs de aplicaciones
y reportes.

Principales Beneficios
MATLAB es un software lder en el computo cientfico y
tcnico, pensado como una plataforma de computo al tiempo
que es un poderoso lenguaje de programacin de alto nivel. Su
creacin ha sido pensada en automatizar clculos y realizar
tareas matemticas complejas de manera interactiva,
estructurada y programtica. por lo que su conocimiento bsico
deriva en una reduccin de tiempo en la realizacin de tareas de
anlisis y visualizacin de datos, desarrollo de aplicaciones
simples y publicacin de resultados.

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-2

Table of Contents
NOSOTROS
Somos una empresa mexicana con ms de 15 aos de experiencia. Fundada
en 1989 para satisfacer la creciente demanda del mercado especializado de
Software Cientfico y Tcnico para la Industria, las Ciencias, los Servicios y la
Educacin de Mxico y Latinoamrica.
Ofrecemos Cursos de MINITAB, MATLAB, MAPLE, entre otros
productos. Nuestra consultora consiste en el desarrollo de aplicaciones
cientficas y tcnicas, integracin de sistemas en las reas de control,
electrnica, mecnica, finanzas, instrumentacin, medicin, adquisicin/
captura de datos en lnea y anlisis en tiempo real para control de procesos.
Visitenos www.multion.com.mx
Mayores informes (Cursos)
Joel Cervantes
MultiON Consulting, SA de CV
Insurgentes Sur 1236-301 Mxico, DF, 03200 MEXICO
Tel.: (55) 5559-4050 ext. 119
Fax: (55) 5559-4048 Country Code: 52
Email: cursos@multion.com.mx

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-3

Table of Contents

COPYRIGHT 2010-2012 by The MathWorks, Inc.

This Training Course Notebook, along with other Training Course Examples
and Exercises, shall at all times remain the intellectual property of The
MathWorks, Inc. The MathWorks, Inc. reserves all rights in these materials.
No part of these materials may be photocopied, reproduced in any form, or
distributed without prior written consent from The MathWorks, Inc.

The software described in this document is furnished under a license


agreement. The software may be used or copied only under the terms of the
license agreement.

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-4

Table of Contents

Table of Contents
1.

Introduction

10. Logic and Flow Control

2.

Working with the MATLAB User Interface

11.

3.

Variables and Expressions

12. Data Analysis

4.

Analysis and Visualization with Vectors

13. Writing Functions

5.

Analysis and Visualization with Matrices

14. Troubleshooting Code and Improving Performance

6.

Automating Commands with Scripts

15. Data Types

7.

Working with Data Files

16. Conclusion

8.

Working with Text and Dates

Appendices

9.

Multiple Vector Plots

2012 by MathWorks, Inc.

Matrix and Image Visualization

MATLAB Fundamentals

TOC-5

Table of Contents

Table of Contents
1. Introduction
MathWorks at a Glance........................................................................................................................................................................................................ 1-2
Worldwide Offices................................................................................................................................................................................................................... 1-3
MathWorks Product Overview...........................................................................................................................................................................................

1-4

Diverse Users............................................................................................................................................................................................................................ 1-5


Computer Setup.......................................................................................................................................................................................................................

1-6

MATLAB?...................................................................................................................................................................................

1-7

Course Learning Outcomes...................................................................................................................................................................................................

1-8

Course Outline.........................................................................................................................................................................................................................

1-9

What Can You Do with

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-6

Table of Contents

Table of Contents
2. Working with the MATLAB User Interface
Outline....................................................................................................................................................................................................................................... 2-2
Chapter 2 Learning Outcomes..............................................................................................................................................................................................

2-3

The MATLAB Desktop.......................................................................................................................................................................................................

2-4

Customizing the Desktop....................................................................................................................................................................................................... 2-5


Course Example: Gas Price Data.......................................................................................................................................................................................... 2-6
Interactive Importing..............................................................................................................................................................................................................

2-7

Variables in the Base Workspace...........................................................................................................................................................................................

2-8

The Variable Editor.................................................................................................................................................................................................................

2-9

New Variables........................................................................................................................................................................................................................... 2-10


Saving and Loading Variables................................................................................................................................................................................................

2-11

Plotting the Data...................................................................................................................................................................................................................... 2-12


Plot Tools..................................................................................................................................................................................................................................

2-13

Multiple Plots...........................................................................................................................................................................................................................

2-14

Formatting the Plot.................................................................................................................................................................................................................

2-15

Data Statistics Tool.................................................................................................................................................................................................................. 2-16


Basic Fitting Tool..................................................................................................................................................................................................................... 2-17
Exporting to Another Application.......................................................................................................................................................................................

2-18

Shortcuts...................................................................................................................................................................................................................................

2-19

Summary.................................................................................................................................................................................................................................... 2-20
Chapter 2 Test Your Knowledge........................................................................................................................................................................................... 2-21
2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-7

Table of Contents

Table of Contents
3. Variables and Expressions
Outline....................................................................................................................................................................................................................................... 3-2
Chapter 3 Learning Outcomes..............................................................................................................................................................................................

3-3

MATLAB Commands..........................................................................................................................................................................................................

3-4

Saving and Loading MAT-Files.............................................................................................................................................................................................

3-5

Assignment...............................................................................................................................................................................................................................

3-6

Creating Vectors....................................................................................................................................................................................................................... 3-7


Creating Matrices.....................................................................................................................................................................................................................

3-8

Matrix Creation Functions.....................................................................................................................................................................................................

3-9

Help and Documentation....................................................................................................................................................................................................... 3-10


Row, Column Indexing...........................................................................................................................................................................................................

3-11

Multiple Row, Column Indices..............................................................................................................................................................................................

3-12

Indexed Assignment................................................................................................................................................................................................................ 3-13


Concatenation..........................................................................................................................................................................................................................

3-14

Characters and Strings............................................................................................................................................................................................................. 3-15


Summary.................................................................................................................................................................................................................................... 3-16
Chapter 3 Test Your Knowledge........................................................................................................................................................................................... 3-17

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-8

Table of Contents

Table of Contents
4. Analysis and Visualization with Vectors
Outline....................................................................................................................................................................................................................................... 4-2
Chapter 4 Learning Outcomes..............................................................................................................................................................................................

4-3

Statistical Functions................................................................................................................................................................................................................. 4-4


Array Operations.....................................................................................................................................................................................................................

4-5

Mathematical Operations.......................................................................................................................................................................................................

4-6

Plotting Vectors........................................................................................................................................................................................................................ 4-7


Plot Options.............................................................................................................................................................................................................................

4-8

Annotating Plots......................................................................................................................................................................................................................

4-9

Additional Vector Plot Types................................................................................................................................................................................................. 4-10


Summary.................................................................................................................................................................................................................................... 4-11
Chapter 4 Test Your Knowledge........................................................................................................................................................................................... 4-13

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-9

Table of Contents

Table of Contents
5. Analysis and Visualization with Matrices
Outline....................................................................................................................................................................................................................................... 5-2
Chapter 5 Learning Outcomes..............................................................................................................................................................................................

5-3

Determining Size.....................................................................................................................................................................................................................

5-4

Matrix Operations.................................................................................................................................................................................................................... 5-5


Array Operations.....................................................................................................................................................................................................................

5-6

Mathematical Operations.......................................................................................................................................................................................................

5-7

Data in the MATLAB Environment.................................................................................................................................................................................. 5-8


Statistical Operations............................................................................................................................................................................................................... 5-9
Plotting Multiple Columns.....................................................................................................................................................................................................

5-10

Reshaping..................................................................................................................................................................................................................................

5-11

Linear Indexing........................................................................................................................................................................................................................

5-12

Multidimensional Arrays......................................................................................................................................................................................................... 5-13


Summary.................................................................................................................................................................................................................................... 5-14
Chapter 5 Test Your Knowledge........................................................................................................................................................................................... 5-15

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-10

Table of Contents

Table of Contents
6. Automating Commands with Scripts
Outline....................................................................................................................................................................................................................................... 6-2
Chapter 6 Learning Outcomes..............................................................................................................................................................................................

6-3

Course Example: Modeling a Whale Call............................................................................................................................................................................

6-4

The Command History........................................................................................................................................................................................................... 6-5


The MATLAB Editor........................................................................................................................................................................................................... 6-6
Script Files................................................................................................................................................................................................................................. 6-7
Running a Script....................................................................................................................................................................................................................... 6-8
Code Sections...........................................................................................................................................................................................................................

6-9

Publishing Code....................................................................................................................................................................................................................... 6-10


Summary.................................................................................................................................................................................................................................... 6-11
Chapter 6 Test Your Knowledge........................................................................................................................................................................................... 6-13

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-11

Table of Contents

Table of Contents
7. Working with Data Files
Outline....................................................................................................................................................................................................................................... 7-2
Chapter 7 Learning Outcomes..............................................................................................................................................................................................

7-3

File Types and File Formats .................................................................................................................................................................................................. 7-4


Reading and Writing Fixed-Format Files.............................................................................................................................................................................

7-5

Interactively Importing Text Files and Spreadsheets........................................................................................................................................................

7-6

Generating File I/O Code...................................................................................................................................................................................................... 7-7


Exporting Spreadsheets..........................................................................................................................................................................................................

7-8

Low-Level File I/O.................................................................................................................................................................................................................

7-9

Summary.................................................................................................................................................................................................................................... 7-10
Chapter 7 Test Your Knowledge........................................................................................................................................................................................... 7-11

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-12

Table of Contents

Table of Contents
8. Working with Text and Dates
Outline....................................................................................................................................................................................................................................... 8-2
Chapter 8 Learning Outcomes..............................................................................................................................................................................................

8-3

Course Example: Car Specifications..................................................................................................................................................................................... 8-4


Cell Arrays................................................................................................................................................................................................................................. 8-5
Cell Indexing............................................................................................................................................................................................................................. 8-6
Converting Array Types.......................................................................................................................................................................................................... 8-7
Converting Numbers and Strings.......................................................................................................................................................................................... 8-8
Course Example: Electricity Consumption.........................................................................................................................................................................

8-9

Working with Dates................................................................................................................................................................................................................. 8-10


Plotting with Dates.................................................................................................................................................................................................................. 8-11
Summary.................................................................................................................................................................................................................................... 8-12
Chapter 8 Test Your Knowledge........................................................................................................................................................................................... 8-13

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-13

Table of Contents

Table of Contents
9. Multiple Vector Plots
Outline....................................................................................................................................................................................................................................... 9-2
Chapter 9 Learning Outcomes..............................................................................................................................................................................................

9-3

Graphics Structure................................................................................................................................................................................................................... 9-4


Multiple Figures.......................................................................................................................................................................................................................

9-5

Multiple Axes............................................................................................................................................................................................................................ 9-6


Multiple Plots...........................................................................................................................................................................................................................

9-7

Plotting Functions...................................................................................................................................................................................................................

9-8

Annotation Techniques........................................................................................................................................................................................................... 9-9


Using Color............................................................................................................................................................................................................................... 9-10
Customizing Plots.................................................................................................................................................................................................................... 9-11
Axis Control.............................................................................................................................................................................................................................

9-12

Summary.................................................................................................................................................................................................................................... 9-13
Chapter 9 Test Your Knowledge........................................................................................................................................................................................... 9-15

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-14

Table of Contents

Table of Contents
10. Logic and Flow Control
Outline....................................................................................................................................................................................................................................... 10-2
Chapter 10 Learning Outcomes............................................................................................................................................................................................

10-3

Logical Operations and Variables.......................................................................................................................................................................................... 10-4


Logical Functions..................................................................................................................................................................................................................... 10-5
Finding and Counting.............................................................................................................................................................................................................

10-6

Logical Indexing....................................................................................................................................................................................................................... 10-7


Programming Constructs.......................................................................................................................................................................................................

10-8

User Interaction.......................................................................................................................................................................................................................

10-9

Flow Control............................................................................................................................................................................................................................

10-10

For-Loops.................................................................................................................................................................................................................................

10-11

While-Loops.............................................................................................................................................................................................................................

10-12

Summary.................................................................................................................................................................................................................................... 10-13
Chapter 10 Test Your Knowledge......................................................................................................................................................................................... 10-15

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-15

Table of Contents

Table of Contents
11. Matrix and Image Visualization
Outline....................................................................................................................................................................................................................................... 11-2
Chapter 11 Learning Outcomes............................................................................................................................................................................................

11-3

Course Example: Ozone Measurements.............................................................................................................................................................................. 11-4


Making Grids............................................................................................................................................................................................................................ 11-5
Interpolating Scattered Data.................................................................................................................................................................................................. 11-6
Surface Plots.............................................................................................................................................................................................................................

11-7

Two-Dimensional Matrix Visualization................................................................................................................................................................................ 11-8


Indexed Images........................................................................................................................................................................................................................

11-9

Colormaps................................................................................................................................................................................................................................. 11-10
True Color Images................................................................................................................................................................................................................... 11-11
Summary.................................................................................................................................................................................................................................... 11-12
Chapter 11 Test Your Knowledge......................................................................................................................................................................................... 11-13

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-16

Table of Contents

Table of Contents
12. Data Analysis
Outline....................................................................................................................................................................................................................................... 12-2
Chapter 12 Learning Outcomes............................................................................................................................................................................................

12-3

Course Example: Modeling Electricity Consumption.......................................................................................................................................................

12-4

Dealing with Missing Data.....................................................................................................................................................................................................

12-5

Piecewise Interpolation........................................................................................................................................................................................................... 12-6


Linear Correlation...................................................................................................................................................................................................................

12-7

Smoothing and Convolution ................................................................................................................................................................................................. 12-8


Polynomial Fitting.................................................................................................................................................................................................................... 12-9
Linear Regression Models......................................................................................................................................................................................................

12-10

Frequency Analysis..................................................................................................................................................................................................................

12-11

FFTs...........................................................................................................................................................................................................................................

12-12

Matrix Equations.....................................................................................................................................................................................................................

12-13

Slash and Backslash.................................................................................................................................................................................................................

12-14

Solving Linear Systems...........................................................................................................................................................................................................

12-15

Summary.................................................................................................................................................................................................................................... 12-16
Chapter 12 Test Your Knowledge......................................................................................................................................................................................... 12-17

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-17

Table of Contents

Table of Contents
13. Writing Functions
Outline....................................................................................................................................................................................................................................... 13-2
Chapter 13 Learning Outcomes............................................................................................................................................................................................

13-3

Increasing Automation...........................................................................................................................................................................................................

13-4

Creating a Function................................................................................................................................................................................................................. 13-5


Calling a Function.................................................................................................................................................................................................................... 13-6
Workspaces............................................................................................................................................................................................................................... 13-7
Subfunctions............................................................................................................................................................................................................................. 13-8
Calling Precedence................................................................................................................................................................................................................... 13-9
The MATLAB Path..............................................................................................................................................................................................................

13-10

Summary.................................................................................................................................................................................................................................... 13-11
Chapter 13 Test Your Knowledge......................................................................................................................................................................................... 13-13

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-18

Table of Contents

Table of Contents
14. Troubleshooting Code and Improving Performance
Outline....................................................................................................................................................................................................................................... 14-2
Chapter 14 Learning Outcomes............................................................................................................................................................................................

14-3

Debugging................................................................................................................................................................................................................................. 14-4
Analyzing Code in the Editor................................................................................................................................................................................................

14-5

Using Breakpoints.................................................................................................................................................................................................................... 14-6


Examining Values....................................................................................................................................................................................................................

14-7

Ending Debugging..................................................................................................................................................................................................................

14-8

Code Performance................................................................................................................................................................................................................... 14-9


Preallocation of Memory........................................................................................................................................................................................................ 14-10
Vectorization............................................................................................................................................................................................................................. 14-11
Summary.................................................................................................................................................................................................................................... 14-12
Chapter 14 Test Your Knowledge......................................................................................................................................................................................... 14-13

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-19

Table of Contents

Table of Contents
15. Data Types
Outline....................................................................................................................................................................................................................................... 15-2
Chapter 15 Learning Outcomes............................................................................................................................................................................................

15-3

MATLAB Data Types........................................................................................................................................................................................................... 15-4


Integer Arrays........................................................................................................................................................................................................................... 15-5
Nondouble Arithmetic............................................................................................................................................................................................................ 15-6
Structures..................................................................................................................................................................................................................................

15-7

Structure Arrays.......................................................................................................................................................................................................................

15-8

Indexing into Structure Arrays..............................................................................................................................................................................................

15-9

Function Handles..................................................................................................................................................................................................................... 15-10


Converting Types.....................................................................................................................................................................................................................

15-11

Summary.................................................................................................................................................................................................................................... 15-12
Chapter 15 Test Your Knowledge......................................................................................................................................................................................... 15-13

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-20

Table of Contents

Table of Contents
Appendices
MATLAB Reference.............................................................................................................................................................................................................

Exercises.................................................................................................................................................................................................................................... B

2012 by MathWorks, Inc.

MATLAB Fundamentals

TOC-21

Table of Contents

MLPR912V2

2012 by MathWorks, Inc.

MATLAB Programming Techniques TOC-1

Table of Contents

Objetivo
El objetivo general del curso es dotar a los asistentes de la
habilidad para la programacin en MATLAB, como un software
de computo tcnico. Al final el curso, los asistentes podrn
programar y realizar aplicaciones diversas que le servirn para
el anlisis y visualizacin de su informacin; as como para
desarrollar programas ms robustos y eficientes.

Principales Beneficios
MATLAB es un software lder en el computo cientfico y
tcnico, pensado como una plataforma de computo al tiempo
que es un poderoso lenguaje de programacin de alto nivel. Su
creacin ha sido pensada en automatizar clculos y realizar
tareas matemticas complejas de manera interactiva,
estructurada y programtica. por lo que su conocimiento bsico
deriva en una reduccin de tiempo en la realizacin de tareas de
anlisis y visualizacin de datos, desarrollo de aplicaciones
simples y publicacin de resultados.

2012 by MathWorks, Inc.

MATLAB Programming Techniques TOC-2

Table of Contents
NOSOTROS
Somos una empresa mexicana con ms de 15 aos de experiencia. Fundada
en 1989 para satisfacer la creciente demanda del mercado especializado de
Software Cientfico y Tcnico para la Industria, las Ciencias, los Servicios y la
Educacin de Mxico y Latinoamrica.
Ofrecemos Cursos de MINITAB, MATLAB, MAPLE, entre otros
productos. Nuestra consultora consiste en el desarrollo de aplicaciones
cientficas y tcnicas, integracin de sistemas en las reas de control,
electrnica, mecnica, finanzas, instrumentacin, medicin, adquisicin/
captura de datos en lnea y anlisis en tiempo real para control de procesos.
Visitenos www.multion.com.mx
Mayores informes (Cursos)
Joel Cervantes
MultiON Consulting, SA de CV
Insurgentes Sur 1236-301 Mxico, DF, 03200 MEXICO
Tel.: (55) 5559-4050 ext. 119
Fax: (55) 5559-4048 Country Code: 52
Email: cursos@multion.com.mx

2012 by MathWorks, Inc.

MATLAB Programming Techniques TOC-3

Table of Contents

COPYRIGHT 2010-2012 by The MathWorks, Inc.

This Training Course Notebook, along with other Training Course Examples
and Exercises, shall at all times remain the intellectual property of The
MathWorks, Inc. The MathWorks, Inc. reserves all rights in these materials.
No part of these materials may be photocopied, reproduced in any form, or
distributed without prior written consent from The MathWorks, Inc.

The software described in this document is furnished under a license


agreement. The software may be used or copied only under the terms of the
license agreement.

2012 by MathWorks, Inc.

MATLAB Programming Techniques TOC-4

Table of Contents

Table of Contents
1.

Introduction

2.

Creating Robust Applications

3.

Development Tools

4.

Structuring Code

5.

Structuring Data

6.

Efficient Data Management

7.

Classes and Objects

8.

Conclusion

Appendices

2012 by MathWorks, Inc.

MATLAB Programming Techniques TOC-5

Table of Contents

Table of Contents
1. Introduction
MathWorks at a Glance........................................................................................................................................................................................................ 1-2
Worldwide Offices................................................................................................................................................................................................................... 1-3
MathWorks Product Overview...........................................................................................................................................................................................

1-4

Diverse Users............................................................................................................................................................................................................................ 1-5


Computer Setup.......................................................................................................................................................................................................................

1-6

Course Learning Outcomes...................................................................................................................................................................................................

1-7

Course Outline.........................................................................................................................................................................................................................

1-8

2012 by MathWorks, Inc.

MATLAB Programming Techniques TOC-6

Table of Contents

Table of Contents
2. Creating Robust Applications
Outline....................................................................................................................................................................................................................................... 2-2
Chapter 2 Learning Outcomes..............................................................................................................................................................................................

2-3

Course Example: Satellite Tracking....................................................................................................................................................................................... 2-4


Setting Default Function Inputs............................................................................................................................................................................................ 2-5
Warnings and Errors...............................................................................................................................................................................................................

2-6

Checking for Error Conditions.............................................................................................................................................................................................

2-7

Flexible Interfaces.................................................................................................................................................................................................................... 2-8


Parsing Property-Value Lists.................................................................................................................................................................................................. 2-9
The try-catch Construct.........................................................................................................................................................................................................

2-10

The MException Object......................................................................................................................................................................................................... 2-11


Summary.................................................................................................................................................................................................................................... 2-12
Chapter 2 Test Your Knowledge........................................................................................................................................................................................... 2-13

2012 by MathWorks, Inc.

MATLAB Programming Techniques TOC-7

Table of Contents

Table of Contents
3. Development Tools
Outline....................................................................................................................................................................................................................................... 3-2
Chapter 3 Learning Outcomes..............................................................................................................................................................................................

3-3

Course Example: Satellite Tracking....................................................................................................................................................................................... 3-4


Directory Tools........................................................................................................................................................................................................................

3-5

Analyzing Code in the Editor................................................................................................................................................................................................

3-6

Resolving Dependencies......................................................................................................................................................................................................... 3-7


Entering Debug Mode............................................................................................................................................................................................................

3-8

Examining Values....................................................................................................................................................................................................................

3-9

Other Considerations.............................................................................................................................................................................................................. 3-10


Diagnosing Performance........................................................................................................................................................................................................ 3-11
MATLAB Profiler.................................................................................................................................................................................................................

3-12

Summary.................................................................................................................................................................................................................................... 3-13
Chapter 3 Test Your Knowledge........................................................................................................................................................................................... 3-14

2012 by MathWorks, Inc.

MATLAB Programming Techniques TOC-8

Table of Contents

Table of Contents
4. Structuring Code
Outline....................................................................................................................................................................................................................................... 4-2
Chapter 4 Learning Outcomes..............................................................................................................................................................................................

4-3

Private Functions.....................................................................................................................................................................................................................

4-4

Subfunctions............................................................................................................................................................................................................................. 4-5
Nested Functions..................................................................................................................................................................................................................... 4-6
Function Handles..................................................................................................................................................................................................................... 4-7
Anonymous Functions............................................................................................................................................................................................................ 4-8
Changing Interface with Anonymous Functions............................................................................................................................................................... 4-9
Precedence Rules.....................................................................................................................................................................................................................

4-10

Comparison of Function Types............................................................................................................................................................................................

4-11

Summary.................................................................................................................................................................................................................................... 4-12
Chapter 4 Test Your Knowledge........................................................................................................................................................................................... 4-13

2012 by MathWorks, Inc.

MATLAB Programming Techniques TOC-9

Table of Contents

Table of Contents
5. Structuring Data
Outline....................................................................................................................................................................................................................................... 5-2
Chapter 5 Learning Outcomes..............................................................................................................................................................................................

5-3

Course Example: Seismic Data ............................................................................................................................................................................................

5-4

Considerations for Structuring Data....................................................................................................................................................................................

5-5

Optimizing File I/O................................................................................................................................................................................................................ 5-6


Cells and Structures................................................................................................................................................................................................................. 5-7
Indexing into Cells and Structures........................................................................................................................................................................................ 5-8
Extraction and Concatenation............................................................................................................................................................................................... 5-9
Converting Cells and Structures............................................................................................................................................................................................ 5-10
Memory Usage.........................................................................................................................................................................................................................

5-11

Other Data Types..................................................................................................................................................................................................................... 5-12


Summary.................................................................................................................................................................................................................................... 5-13
Chapter 5 Test Your Knowledge........................................................................................................................................................................................... 5-14

2012 by MathWorks, Inc.

MATLAB Programming Techniques TOC-10

Table of Contents

Table of Contents
6. Efficient Data Management
Outline....................................................................................................................................................................................................................................... 6-2
Chapter 6 Learning Outcomes..............................................................................................................................................................................................

6-3

Preallocation.............................................................................................................................................................................................................................

6-4

Preallocating Cells and Structures.........................................................................................................................................................................................

6-5

Vectorization............................................................................................................................................................................................................................. 6-6
Applying Scalar Functions to Arrays....................................................................................................................................................................................

6-7

Vectorizing and Memory........................................................................................................................................................................................................

6-8

Copy-on-Write Behavior......................................................................................................................................................................................................... 6-9


In-Place Optimizations...........................................................................................................................................................................................................

6-10

Memory Anatomy.................................................................................................................................................................................................................... 6-11


Summary.................................................................................................................................................................................................................................... 6-12
Chapter 6 Test Your Knowledge........................................................................................................................................................................................... 6-13

2012 by MathWorks, Inc.

MATLAB Programming Techniques TOC-11

Table of Contents

Table of Contents
7. Classes and Objects
Outline....................................................................................................................................................................................................................................... 7-2
Chapter 7 Learning Outcomes..............................................................................................................................................................................................

7-3

Custom Data Types.................................................................................................................................................................................................................

7-4

Course Example: A Lookup Table........................................................................................................................................................................................ 7-5


Why Write a Class?..................................................................................................................................................................................................................

7-6

Class Definition Files..............................................................................................................................................................................................................

7-7

Properties..................................................................................................................................................................................................................................

7-8

Methods..................................................................................................................................................................................................................................... 7-9
Property Attributes.................................................................................................................................................................................................................. 7-10
Creating a Table Object..........................................................................................................................................................................................................

7-11

The Constructor Method.......................................................................................................................................................................................................

7-12

Handle Classes.......................................................................................................................................................................................................................... 7-13


Precedence Rules.....................................................................................................................................................................................................................

7-14

Overloading Methods.............................................................................................................................................................................................................. 7-15


Overloading Operators...........................................................................................................................................................................................................

7-16

Code Reuse...............................................................................................................................................................................................................................

7-17

Summary.................................................................................................................................................................................................................................... 7-18
Chapter 7 Test Your Knowledge........................................................................................................................................................................................... 7-19

2012 by MathWorks, Inc.

MATLAB Programming Techniques TOC-12

Table of Contents

Table of Contents
Appendices
Exercises.................................................................................................................................................................................................................................... A

2012 by MathWorks, Inc.

MATLAB Programming Techniques TOC-13

Das könnte Ihnen auch gefallen