• Nu S-Au Găsit Rezultate

508-647-7001 (Fax) The MathWorks, Inc.

N/A
N/A
Protected

Academic year: 2022

Share "508-647-7001 (Fax) The MathWorks, Inc."

Copied!
689
0
0

Text complet

(1)

Graphics

R2011b

(2)

Technical Support

[email protected] Product enhancement suggestions [email protected] Bug reports

[email protected] Documentation error reports

[email protected] Order status, license renewals, passcodes [email protected] Sales, pricing, and general information

508-647-7000 (Phone)

508-647-7001 (Fax) The MathWorks, Inc.

3 Apple Hill Drive Natick, MA 01760-2098

For contact information about worldwide offices, see the MathWorks Web site.

MATLAB®Graphics

© COPYRIGHT 1984–2011 by 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. No part of this manual may be photocopied or reproduced in any form without prior written consent from The MathWorks, Inc.

FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by, for, or through the federal government of the United States. By accepting delivery of the Program or Documentation, the government hereby agrees that this software or documentation qualifies as commercial computer software or commercial computer software documentation as such terms are used or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and govern the use, modification, reproduction, release, performance, display, and disclosure of the Program and Documentation by the federal government (or other entity acquiring for or through the federal government) and shall supersede any conflicting contractual terms or conditions. If this License fails to meet the government’s needs or is inconsistent in any respect with federal procurement law, the government agrees to return the Program and Documentation, unused, to The MathWorks, Inc.

Trademarks

MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See

www.mathworks.com/trademarksfor a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders.

Patents

MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patentsfor more information.

(3)

This publication was previously part of the Using MATLAB®Graphics User Guide.

October 2008 Online only Revised for MATLAB 7.7 (Release 2008b) March 2009 Online only Revised for MATLAB 7.8 (Release 2009a) September 2009 Online only Revised for MATLAB 7.9 (Release 2009b) March 2010 Online only Revised for MATLAB 7.10 (Release 2010a) September 2010 Online only Revised for MATLAB 7.11 (Release 2010b) April 2011 Online only Revised for MATLAB 7.12 (Release 2011a) September 2011 Online only Revised for MATLAB 7.13 (Release 2011b)

(4)
(5)

Plots and Plotting Tools

1

Figures, Plots, and Graphs

. . . .

1-2 What Is a MATLAB Graph?

. . . .

1-2 Anatomy of a Graph

. . . .

1-3 Figure Toolbars

. . . .

1-5 Types of MATLAB Plots

. . . .

1-6 Plotting Tools — Interactive Plotting

. . . .

1-11 What Are Plotting Tools?

. . . .

1-11 Plotting Tools Interface Overview

. . . .

1-12 The Figure Palette

. . . .

1-19 The Plot Browser

. . . .

1-24 The Property Editor

. . . .

1-29 Accessing Object Properties with the Property Inspector

. .

1-30 Example — Plotting Workspace Variables

. . . .

1-36 Identifying Workspace Data to Plot

. . . .

1-36 Adding a Subplot

. . . .

1-39 Example — Choosing a Graph Type

. . . .

1-44 Selecting a Graph from the Plot Catalog

. . . .

1-44 Plotting Expressions

. . . .

1-48 Example — Specifying a Data Source

. . . .

1-52 Creating the Graph

. . . .

1-52 Varying the Data Source

. . . .

1-52 Data Sources for Multiobject Graphs

. . . .

1-54 Example — Generating MATLAB Code to Reproduce a

Graph

. . . .

1-56 Create a Stem Plot and Generate Code for It

. . . .

1-56 Data Arguments

. . . .

1-58 Limitations

. . . .

1-58

(6)

Working in Plot Edit Mode

. . . .

1-61 Figure Windows in Plot Edit Mode

. . . .

1-61 Starting Plot Edit Mode

. . . .

1-62 Exiting Plot Edit Mode

. . . .

1-63 Selecting Objects in a Graph

. . . .

1-63 Cutting, Copying, and Pasting Plot Objects

. . . .

1-64 Moving and Resizing Objects

. . . .

1-67 Setting Object Properties

. . . .

1-68 Undo/Redo — Eliminating Mistakes

. . . .

1-68 Saving Your Work

. . . .

1-70 Saving a Graph in FIG-File Format

. . . .

1-70 Saving to a Different Format — Exporting Figures

. . . .

1-71 Printing Figures

. . . .

1-72 Generating a MATLAB File to Recreate a Graph

. . . .

1-73

Data Exploration Tools

2

Ways to Explore Graphical Data

. . . .

2-2 Introduction

. . . .

2-2 Types of Tools

. . . .

2-2 Customizing Data Exploration Tools

. . . .

2-3 Data Cursor — Displaying Data Values Interactively

. .

2-10 What Is a Data Cursor?

. . . .

2-10 Enabling Data Cursor Mode

. . . .

2-11 Display Style — Datatip or Cursor Window

. . . .

2-20 Selection Style — Select Data Points or Interpolate Points

on Graph

. . . .

2-21 Exporting Data Value to Workspace Variable

. . . .

2-22 Enlarging the View

. . . .

2-25 Zooming in 2-D and 3-D

. . . .

2-25

(7)

Rotate 3D — Interactive Rotation of 3-D Views

. . . .

2-31 Enabling 3-D Rotation

. . . .

2-31 Selecting Predefined Views

. . . .

2-31 Rotation Style for Complex Graphs

. . . .

2-32 Undo/Redo — Eliminating Mistakes

. . . .

2-34

Annotating Graphs

3

How to Annotate Graphs

. . . .

3-2 Graph Annotation Features

. . . .

3-2 Enclosing Regions of a Graph in a Rectangle or an

Ellipse

. . . .

3-6 Textbox Annotations

. . . .

3-8 Annotation Lines and Arrows

. . . .

3-12 Adding a Colorbar to a Graph

. . . .

3-15 Adding a Legend to a Graph

. . . .

3-19 Pinning — Attaching to a Point in the Graph

. . . .

3-22 Alignment Tool — Aligning and Distributing Objects

. .

3-24 Alignment Tool Functionality

. . . .

3-24 Example — Vertical Distribute, Horizontal Align

. . . .

3-25 Align/Distribute Menu Options

. . . .

3-28 Snap to Grid — Aligning Objects on a Grid

. . . .

3-30 Adding Titles to Graphs

. . . .

3-33 What Is a Title?

. . . .

3-33 Using the Title Option on the Insert Menu

. . . .

3-34 Using the Property Editor to Add a Title

. . . .

3-34 Using the title Function

. . . .

3-35 Adding Axis Labels to Graphs

. . . .

3-38 What Are Axis Labels?

. . . .

3-38 Using the Label Options on the Insert Menu

. . . .

3-40

(8)

What Are Text Annotations?

. . . .

3-45 Creating Text Annotations with the text or gtext

Function

. . . .

3-46 Text Alignment

. . . .

3-51 Example — Aligning Text

. . . .

3-52 Editing Text Objects

. . . .

3-54 Mathematical Symbols, Greek Letters, and TeX

Characters

. . . .

3-54 Using Character and Numeric Variables in Text

. . . .

3-58 Example — Multiline Text

. . . .

3-59 Example — Using LaTeX to Format Math Equations

. . . .

3-60 Drawing Text in a Box

. . . .

3-64 Adding Arrows and Lines to Graphs

. . . .

3-66 Creating Arrows and Lines in Plot Editing Mode

. . . .

3-66 Editing Arrows and Line Annotations

. . . .

3-67 Positioning Annotations in Data Space

. . . .

3-69 Example — Pinning Text Arrows and Ellipses

. . . .

3-69

Basic Plotting Commands

4

Setting Up Figures

. . . .

4-2 Creating Figure Windows

. . . .

4-2 Displaying Multiple Plots per Figure

. . . .

4-2 Specifying the Target Axes

. . . .

4-5 Default Color Scheme

. . . .

4-5 Using High-Level Plotting Functions

. . . .

4-7 Functions for Plotting Line Graphs

. . . .

4-7 Programmatic Plotting

. . . .

4-8 Creating Line Plots

. . . .

4-9 Specifying Line Style

. . . .

4-11 Colors, Line Styles, and Markers

. . . .

4-12

(9)

Line Styles for Black and White Output

. . . .

4-17 Setting Default Line Styles

. . . .

4-18 Line Plots of Matrix Data

. . . .

4-20 Plotting Imaginary and Complex Data

. . . .

4-23 Plotting with Two Y-Axes

. . . .

4-25 Introduction

. . . .

4-25 Combining Linear and Logarithmic Axes

. . . .

4-26 Setting Axis Parameters

. . . .

4-29 Axis Scaling and Ticks

. . . .

4-29 Axis Limits and Ticks

. . . .

4-29 Example — Specifying Ticks and Tick Labels

. . . .

4-32 Setting Aspect Ratio

. . . .

4-34

Creating Specialized Plots

5

Bar and Area Graphs

. . . .

5-2 Types of Bar Graphs

. . . .

5-2 Coloring 2-D Bars According to Height

. . . .

5-6 Coloring 3-D Bars According to Height

. . . .

5-10 Stacked Bar Graphs to Show Contributing Amounts

. . . . .

5-12 Specifying X-Axis Data

. . . .

5-14 Overlaying Bar Graphs

. . . .

5-16 Overlaying Other Plots on Bar Graphs

. . . .

5-17 Area Graphs

. . . .

5-21 Comparing Data Sets with Area Graphs

. . . .

5-24 Pie Charts

. . . .

5-28 Creating a Pie Chart

. . . .

5-28 Labeling the Pie Chart

. . . .

5-29

(10)

Histograms in Cartesian Coordinates

. . . .

5-33 Histograms in Polar Coordinates

. . . .

5-35 Specifying Number of Bins

. . . .

5-36 Using Data Cursors with Histograms

. . . .

5-38 Discrete Data Graphs

. . . .

5-40 Functions for Creating Graphs of Discrete Data

. . . .

5-40 Two-Dimensional Stem Plots

. . . .

5-40 Combining Stem Plots with Line Plots

. . . .

5-44 Three-Dimensional Stem Plots

. . . .

5-45 Stairstep Plots

. . . .

5-48 Direction and Velocity Vector Graphs

. . . .

5-51 Functions for Graphing Vector Quantities

. . . .

5-51 Compass Plots

. . . .

5-52 Feather Plots

. . . .

5-53 Two-Dimensional Quiver Plots

. . . .

5-55 Three-Dimensional Quiver Plots

. . . .

5-57 Contour Plots

. . . .

5-60 Functions for Creating Contour Displays

. . . .

5-60 Creating Simple Contour Plots

. . . .

5-61 Labeling Contours

. . . .

5-63 Filled Contours

. . . .

5-65 Specifying Contour Levels

. . . .

5-66 Index Contours

. . . .

5-70 The Contouring Algorithm

. . . .

5-73 Changing the Offset of a Contour

. . . .

5-76 Displaying Contours in Polar Coordinates

. . . .

5-77 Preparing Data for Contouring

. . . .

5-80 Interactive Plotting

. . . .

5-84 Example — Selecting Plotting Points from the Screen

. . . .

5-84 Animation

. . . .

5-86 Ways to Animate Plots

. . . .

5-86 Movies

. . . .

5-87

(11)

Displaying Bit-Mapped Images

6

Working with Images in MATLAB Graphics

. . . .

6-2 What Is Image Data?

. . . .

6-2 Supported Image Formats

. . . .

6-3 Functions for Reading, Writing, and Displaying Images

. .

6-4 Image Types

. . . .

6-5 Indexed Images

. . . .

6-5 Intensity Images

. . . .

6-7 RGB (Truecolor) Images

. . . .

6-8 Working with 8-Bit and 16-Bit Images

. . . .

6-10 8-Bit and 16-Bit Indexed Images

. . . .

6-10 8-Bit and 16-Bit Intensity Images

. . . .

6-11 8-Bit and 16-Bit RGB Images

. . . .

6-11 Mathematical Operations Support for uint8 and uint16

. .

6-12 Other 8-Bit and 16-Bit Array Support

. . . .

6-13 Converting an 8-Bit RGB Image to Grayscale

. . . .

6-13 Summary of Image Types and Numeric Classes

. . . .

6-17 Reading, Writing, and Querying Graphics Image

Files

. . . .

6-18 Working with Image Formats

. . . .

6-18 Reading a Graphics Image

. . . .

6-19 Writing a Graphics Image

. . . .

6-19 Subsetting a Graphics Image (Cropping)

. . . .

6-20 Obtaining Information About Graphics Files

. . . .

6-21 Displaying Graphics Images

. . . .

6-22 Image Types and Display Methods

. . . .

6-22 Controlling Aspect Ratio and Display Size

. . . .

6-24 The Image Object and Its Properties

. . . .

6-27

(12)

Printing Images

. . . .

6-36 Converting the Data or Graphic Type of Images

. . . .

6-37

Printing and Exporting

7

Overview of Printing and Exporting

. . . .

7-2 Print and Export Operations

. . . .

7-2 Graphical User Interfaces

. . . .

7-2 Command Line Interface

. . . .

7-3 Specifying Parameters and Options

. . . .

7-5 Default Settings and How to Change Them

. . . .

7-7 How to Print or Export

. . . .

7-10 Using Print Preview

. . . .

7-10 Printing a Figure

. . . .

7-13 Printing to a File

. . . .

7-18 Exporting to a File

. . . .

7-20 Exporting to the Windows or Macintosh Clipboard

. . . .

7-32 Examples of Printing and Exporting

. . . .

7-37 Printing a Figure at Screen Size

. . . .

7-37 Printing with a Specific Paper Size

. . . .

7-38 Printing a Centered Figure

. . . .

7-38 Exporting in a Specific Graphics Format

. . . .

7-40 Exporting in EPS Format with a TIFF Preview

. . . .

7-41 Exporting a Figure to the Clipboard

. . . .

7-41 Changing a Figure’s Settings

. . . .

7-44 Parameters that Affect Printing

. . . .

7-44 Selecting the Figure

. . . .

7-46

(13)

Selecting a Renderer

. . . .

7-55 Setting the Resolution

. . . .

7-61 Setting the Axes Ticks and Limits

. . . .

7-64 Setting the Background Color

. . . .

7-66 Setting Line and Text Characteristics

. . . .

7-67 Setting the Line and Text Color

. . . .

7-70 Specifying a Colorspace for Printing and Exporting

. . . .

7-73 Excluding User Interface Controls form Printed Output

. .

7-75 Producing Uncropped Figures

. . . .

7-76 Choosing a Graphics Format

. . . .

7-77 What Are Graphic Formats?

. . . .

7-77 Frequently Used Graphics Formats

. . . .

7-78 Factors to Consider in Choosing a Format

. . . .

7-78 Properties Affected by Choice of Format

. . . .

7-81 Impact of Rendering Method on the Output

. . . .

7-83 Description of Selected Graphics Formats

. . . .

7-84 How to Specify a Format for Exporting

. . . .

7-87 Choosing a Printer Driver

. . . .

7-89 What Are Printer Drivers?

. . . .

7-89 Factors to Consider in Choosing a Driver

. . . .

7-90 Driver-Specific Information

. . . .

7-93 How to Specify the Printer Driver to Use

. . . .

7-97 Troubleshooting

. . . .

7-99 Introduction

. . . .

7-99 Common Problems

. . . .

7-99 Printing Problems

. . . .

7-100 Exporting Problems

. . . .

7-103 General Problems

. . . .

7-107

(14)

Organization of Graphics Objects

. . . .

8-3 Types of Graphics Objects

. . . .

8-4 Introduction

. . . .

8-4 Information on Specific Graphics Objects

. . . .

8-4 Graphics Windows — the Figure

. . . .

8-6 Introduction

. . . .

8-6 Figures Used for Graphing Data

. . . .

8-7 Figures Used for GUIs

. . . .

8-8 Root Object — The Figure Parent

. . . .

8-9 More Information on Figures

. . . .

8-9 Core Graphics Objects

. . . .

8-10 Introduction

. . . .

8-10 Core Graphics Objects

. . . .

8-13 Example — Creating Core Graphics Objects

. . . .

8-14 Parenting

. . . .

8-16 High-Level Versus Low-Level Functions

. . . .

8-17 Simplified Calling Syntax

. . . .

8-17 Plot Objects

. . . .

8-19 Introduction

. . . .

8-19 Creating a Plot Object

. . . .

8-20 Identifying Plot Objects Programmatically

. . . .

8-21 Plot Objects and Backward Compatibility

. . . .

8-22 Linking Graphs to Variables — Data Source

Properties

. . . .

8-23 Introduction

. . . .

8-23 Data Source Example

. . . .

8-23 Changing the Size of Data Variables

. . . .

8-24 Annotation Objects

. . . .

8-25 Introduction

. . . .

8-25 Annotation Object Properties

. . . .

8-25 Annotation Layer

. . . .

8-26

(15)

Introduction

. . . .

8-30 Creating a Group

. . . .

8-30 Transforming Objects

. . . .

8-31 Example — Transforming a Hierarchy of Objects

. . . . .

8-39 Object Properties

. . . .

8-44 Introduction

. . . .

8-44 Storing Object Information

. . . .

8-44 Changing Values

. . . .

8-45 Order Dependence of Setting Property Values

. . . .

8-45 Default Values

. . . .

8-46 Properties Common to All Objects

. . . .

8-46 Setting and Querying Property Values

. . . .

8-48 Using set and get

. . . .

8-48 Setting Property Values

. . . .

8-48 Querying Property Values

. . . .

8-50 Factory-Defined Property Values

. . . .

8-53 Setting Default Property Values

. . . .

8-54 Factory- and User-Defined Values

. . . .

8-54 How MATLAB Searches for Default Values

. . . .

8-54 Defining Default Values

. . . .

8-56 Examples — Setting Default Line Styles

. . . .

8-57 Accessing Object Handles

. . . .

8-61 Introduction

. . . .

8-61 Special Object Handles

. . . .

8-61 The Current Figure, Axes, and Object

. . . .

8-62 Searching for Objects by Property Values — findobj

. . . . .

8-63 Copying Objects

. . . .

8-68 Deleting Objects

. . . .

8-70 Controlling Graphics Output

. . . .

8-72

(16)

Testing for Hold State

. . . .

8-79 Protecting Figures and Axes

. . . .

8-80 The Figure Close Request Function

. . . .

8-83 Introduction

. . . .

8-83 Quitting the MATLAB Environment

. . . .

8-84 Errors in the Close Request Function

. . . .

8-84 Overriding the Close Request Function

. . . .

8-85 Redefining the CloseRequestFcn

. . . .

8-85 Saving Handles in Files

. . . .

8-87 About Saving Handles

. . . .

8-87 Save Information First

. . . .

8-87 Properties Changed by Built-In Functions

. . . .

8-89 Objects That Can Contain Other Objects

. . . .

8-92 Using Panel Containers in Figures — Uipanels

. . . .

8-93 Introduction

. . . .

8-93 Figure Resize Functions

. . . .

8-93 Example — Using Figure Panels

. . . .

8-94 Grouping Objects Within Axes — hgtransform

. . . .

8-99 Introduction

. . . .

8-99 Example — Translating Grouped Objects

. . . .

8-99 Controlling Legends

. . . .

8-103 Legend Control Options

. . . .

8-103 Properties for Controlling Legend Content

. . . .

8-103 Updating a Legend

. . . .

8-104 Example — Excluding a Particular Object From a

Legend

. . . .

8-105 Example — One Legend Entry for a Group of Objects

. . . .

8-106 Example — Showing Children of Group Objects in

Legend

. . . .

8-107

(17)

What is a Callback?

. . . .

8-111 Graphics Object Callbacks

. . . .

8-111 User Interface Object Callbacks

. . . .

8-112 Figure Callbacks

. . . .

8-112 Function Handle Callbacks

. . . .

8-113 Introduction

. . . .

8-113 Function Handle Syntax

. . . .

8-114 Why Use Function Handle Callbacks

. . . .

8-115 Example — Using Function Handles in GUIs

. . . .

8-117 Optimizing Graphics Performance

. . . .

8-122 Introduction

. . . .

8-122 General Performance Guidelines

. . . .

8-122 Disabling Automatic Modes

. . . .

8-123 Changing Graph Data Rapidly

. . . .

8-125 Specify Axes with Plotting Function for Better

Performance

. . . .

8-128 Performance of Bit-Mapped Images

. . . .

8-129 Performance of Patch Objects

. . . .

8-130 Performance of Surface Objects

. . . .

8-131

Using Figure Properties

9

Figure Objects

. . . .

9-2 Related Information About Figures

. . . .

9-2 Docking Figures in the Desktop

. . . .

9-3 Introduction

. . . .

9-3 Figure Properties That Affect Docking

. . . .

9-4 Creating a Nondockable Figure

. . . .

9-5 Positioning Figures

. . . .

9-6

(18)

Figure Colormaps — The Colormap Property

. . . .

9-12 Introduction

. . . .

9-12 Specifying the Figure Colormap

. . . .

9-12 Selecting Drawing Methods

. . . .

9-14 Double Buffering

. . . .

9-14 Selecting a Renderer

. . . .

9-14 Specifying the Figure Pointer

. . . .

9-17 Predefined Figure Pointer Symbols

. . . .

9-17 Defining Custom Pointers

. . . .

9-18

Using Axes Properties

10

Axes Objects — Defining Coordinate Systems for

Graphs

. . . .

10-2 Labeling and Appearance Properties

. . . .

10-3 Introduction

. . . .

10-3 Creating Axes with Specific Characteristics

. . . .

10-3 Axis Labels

. . . .

10-4 Positioning Axes

. . . .

10-6 Introduction

. . . .

10-6 The Position Vector

. . . .

10-6 Position Units

. . . .

10-8 Automatic Axes Resize

. . . .

10-9 Properties Controlling Axes Size

. . . .

10-9 Using OuterPosition as the ActivePositionProperty

. . . .

10-11 ActivePositionProperty = OuterPosition

. . . .

10-12 ActivePositionProperty = Position

. . . .

10-12 Axes Resizing in Subplots

. . . .

10-13

(19)

Individual Axis Control

. . . .

10-18 Properties Controlling Axis Limits

. . . .

10-18 Setting Axis Limits

. . . .

10-19 Setting Tick Mark Locations

. . . .

10-20 Changing Axis Direction

. . . .

10-22 Using Multiple X- and Y-Axes

. . . .

10-25 Introduction

. . . .

10-25 Example — Double Axis Graphs

. . . .

10-25 Automatic-Mode Properties

. . . .

10-29 Colors Controlled by Axes

. . . .

10-32 Introduction

. . . .

10-32 Specifying Axes Colors

. . . .

10-32 Axes Color Limits — the CLim Property

. . . .

10-36 Introduction

. . . .

10-36 Simulating Multiple Colormaps in a Figure

. . . .

10-37 Complete Example Code

. . . .

10-37 Calculating Color Limits

. . . .

10-38 Defining the Color of Lines for Plotting

. . . .

10-41 Introduction

. . . .

10-41 Defining Your Own ColorOrder

. . . .

10-41 Line Styles Used for Plotting — LineStyleOrder

. . . .

10-43

Index

(20)
(21)

Plots and Plotting Tools

If you are viewing this document in the MATLAB®Help browser, watch the Interactive Plot Creation with the Plot Tools video demo for an overview of the major functionality. It covers much of the material presented in the following sections:

“Figures, Plots, and Graphs” on page 1-2

“Plotting Tools — Interactive Plotting” on page 1-11

“Example — Plotting Workspace Variables” on page 1-36

“Example — Choosing a Graph Type” on page 1-44

“Example — Specifying a Data Source” on page 1-52

“Example — Generating MATLAB Code to Reproduce a Graph” on page 1-56

“Editing Plots” on page 1-59

“Working in Plot Edit Mode” on page 1-61

“Saving Your Work” on page 1-70

(22)

Figures, Plots, and Graphs

In this section...

“What Is a MATLAB Graph?” on page 1-2

“Anatomy of a Graph” on page 1-3

“Figure Toolbars” on page 1-5

“Types of MATLAB Plots” on page 1-6

What Is a MATLAB Graph?

The MATLAB environment offers a variety of data plotting functions plus a set of GUI tools to create, and modify graphic displays. The GUI tools afford most of the control over graphic properties and options that typed commands such asannotate,get, andsetprovide.

Afigureis a MATLAB window that contains graphic displays (usually data plots) and UI components. You create figures explicitly with thefigure function, and implicitly whenever you plot graphics and no figure is active.

By default, figure windows are resizable and include pull-down menus and toolbars.

Aplotis any graphic display you can create within a figure window. Plots can display tabular data, geometric objects, surface and image objects, and annotations such as titles, legends, and colorbars. Figures can contain any number of plots. Each plot is created within a 2-D or a 3-D data space called anaxes. You can explicitly create axes with theaxesorsubplotfunctions.

Agraphis a plot ofdatawithin a 2-D or 3-D axes. Most plots made with MATLAB functions and GUIs are therefore graphs. When you graph a one-dimensional variable (e.g.,rand(100,1)), the indices of the data vector (in this case1:100) become assigned asxvalues, and plots the data vector asyvalues. Some types of graphs can display more than one variable at a time, others cannot.

The contents and varieties of figures, plots and graphs that MATLAB can make are explained in the following sections.

(23)

Note All the figures are generated on a Windows®system, the placement of the toolbar and menu options can vary for other operating systems.

Anatomy of a Graph

MATLAB plotting functions and tools direct their output to a figure window.

Each figure is a separate window that you can dock in the desktop, and collect together with other plots in aFigure Group. To illustrate the basic components of a graph, execute the following code to create a plot of a family of sine curves:

x = 0:.2:20;

y = sin(x)./sqrt(x+1);

y(2,:) = sin(x/2)./sqrt(x+1);

y(3,:) = sin(x/3)./sqrt(x+1);

plot(x,y)

The resulting figure contains a 2-D set of axes. This graphic identifies the components and tools of a figure window.

(24)

Dock figure in MATLAB desktop MATLAB

figure window One of the figure

toolbars

Line plots representing data Axes in which

MATLAB plots data

Theplotfunction uses a default line style and color to distinguish the data sets plotted in the graph. You can change the appearance of these graphic components or add annotations to the graph to present your data in a particular way.

(25)

Figure Toolbars

Figure toolbars provide shortcuts to access commonly used features. These include operations such as saving and printing, plus tools for interactive zooming, panning, rotating, querying, and editing plots. The following picture shows the features available from this toolbar.

Insert

color bar Insert

legend

Hide/display plot tools Enable plot

edit mode

Pan Rotate 3-D Data

cursor Zoomin/out

brushData Data link

You can enable two other toolbars from theViewmenu:

Camera Toolbar — Use for manipulating 3-D views. See “View Control with the Camera Toolbar” in the MATLAB 3-D Visualization documentation for more information.

Camera Motion

Controls Principal Axis

Selector Scene

Light Projection

Type Reset and Stop

Plot Edit Toolbar — Use for annotation and setting object properties.

See “Annotation Tools on the Plot Edit Toolbar” on page 3-3 for more information.

(26)

Click this button to enable property

editing of graphic objects. Display the object

alignment tool Pin object to

data point

Fill color and line/

edge color

Text color, font,

bold or italics Align text Insert lines

and arrows Insert textarrow, text, rectangle and ellipse

Types of MATLAB Plots

You can construct a wide variety of 2-D and 3-D MATLAB plots with very little, if any, programming required on your part. The following two tables classify and illustrate most of the kinds of plots you can create. They include line, bar, area, direction and vector field, radial, and scatter graphs. They also include 2-D and 3-D functions that generate and plot geometric shapes and objects. Most 2-D plots have 3-D analogs, and there are a variety of volumetric displays for 3-D solids and vector fields. Plot types that begin with “ez” (such asezsurf) are convenience functions that can plot arguments given as functions.

Two-Dimensional Plotting Functions

The table below shows all available MATLAB 2-D plot functions. If you are reading this online, you can click any icon to see the documentation for that function. Techniques for using many of the functions are also discussed in later sections of this document.

(27)

Line Graphs

Bar Graphs

Area Graphs

Direction Graphs

Radial Graphs

Scatter Graphs

plot bar

(grouped)

area feather polar scatter

plotyy barh (grouped)

pie quiver rose spy

loglog bar (stacked)

fill comet compass plotmatrix

semilogx barh (stacked)

contourf ezpolar

semilogy hist image

stairs pareto pcolor

contour errorbar ezcontourf

(28)

Line Graphs

Bar Graphs

Area Graphs

Direction Graphs

Radial Graphs

Scatter Graphs ezplot stem

ezcontour

Three-Dimensional Plotting Functions

The table below shows all available MATLAB 3-D and volumetric plot functions. It includes functions that generate 3-D data (cylinder,ellipsoid, sphere), but most plot either arrays of data or functions. If you are reading this online, you can click any picture in the table to see the documentation for that function. For information about and examples of using 3-D plotting functions, see “Creating 3-D Graphs” in the 3-D Visualization documentation.

Line Graphs

Mesh Graphs and Bar

Graphs

Area Graphs

and Constructive

Objects

Surface Graphs

Direction Graphs

Volumetric Graphs

plot3 mesh pie3 surf quiver3 scatter3

contour3 meshc fill3 surfl comet3 coneplot

contourslicemeshz patch surfc streamslicestreamline

(29)

Line Graphs

Mesh Graphs and Bar

Graphs

Area Graphs

and Constructive

Objects

Surface Graphs

Direction Graphs

Volumetric Graphs

ezplot3 ezmesh cylinder ezsurf streamribbon

waterfall stem3 ellipsoid ezsurfc streamtube

bar3 sphere

bar3h

Choosing a Plot Type Interactively

You can interactively generate any of the plotting functions shown in the previous tables using the Plot Selector and Plot Catalog tools:

1 Select one or more numeric workspace variables in the Workspace Browser, the Variable Editor or the plotting tools Figure Palette.

2 Open the Plot Selector or Plot Catalog tool and scroll to the name or icon of the type of graph you want to create. Alternatively, right-click a selected variable and choosePlot Catalogfrom the context menu. You can enter a search term in either tool to locate a graph by function name or other identifying information.

(30)

3 Click the icon to plot the selected variables.

The graph displays in the current figure, and the command that generated it displays in the Command Window.

You can choose any type of MATLAB plot and plots from several toolboxes.

You need a license for a toolbox in order for its plot types to display in the tools. If you select a graphics function that is not able to plot the selected variables, the entry for that plot type turns gray and a diagnostic message pops up when you hover over it.

For more information, see , , “Creating Plots from the Workspace Browser”, and “Plotting Workspace Variables” on page 1-20.

(31)

Plotting Tools — Interactive Plotting

In this section...

“What Are Plotting Tools?” on page 1-11

“Plotting Tools Interface Overview” on page 1-12

“The Figure Palette” on page 1-19

“The Plot Browser” on page 1-24

“The Property Editor” on page 1-29

“Accessing Object Properties with the Property Inspector” on page 1-30

What Are Plotting Tools?

The modular, interactive plotting environment calledplotting toolsenables you to

Create various type of graphs

Select variables to plot directly from a workspace browser

Easily create and manipulate subplots in the figure

Add annotations such as arrows, lines, and text

Set properties on graphics objects

You can open and configure plotting tools in many ways. To create a figure with the plotting tools attached, use theplottoolscommand. You can also start the plotting tools from the figure toolbar by clicking theShow Plot Toolsicon .

Remove the plotting tools from a figure using the Hide Plot Tools icon . You can display the three basic plotting tools from theViewmenu by selectingFigure Palette,Plot Browser, orProperty Editor.

The next section describes the individual components making up the plotting tools.

(32)

Plotting Tools Interface Overview

The Plotting Tools interface includes three panels that are associated with a figure.

Figure Palette — Use to create and arrange subplot axes, view and plot workspace variables, and add annotations. Display the Figure Palette using thefigurepalettecommand.

Plot Browser — Use to select and control the visibility of the axes or graphic objects plotted in the figure. You can also add data to any selected axes by clicking theAdd Databutton. Display the Plot Browser using theplotbrowsercommand.

Property Editor — Use to set common properties of the selected object. You can also open the Property Editor using thepropertyeditorcommand. In the Property Editor you can click theMore Propertiesbutton to display the Property Inspector, a GUI that displays most object properties and allows you to change any property’s value (unless it is read-only). See

“Accessing Object Properties with the Property Inspector” on page 1-30 for details.

(33)

Activating Plotting Tools

The following example shows the plotting tools attached to a figure containing two subplots of lineseries data. The code to produce the graphs is

% First subplot x = 0:pi/100:2*pi;

y1 = sin(x);

y2 = sin(x+.25);

y3 = sin(x+.5);

subplot(2,1,1);

plot(x,y1,x,y2,x,y3);

axis tight;

% Second sublot w1 = cos(x);

w2 = cos(x+.25);

w3 = cos(x+.5);

subplot(2,1,2);

plot(x,w1,x,w2,x,w3);

axis tight;

You summon the plotting tools, either by selectingFigure Palette,Plot Browser, andProperty Editorfrom the figure’sViewmenu, or by typing plottools

in the Command Window. Typingplottoolsorplottools onrestores the configuration of tools the last time you were using them; use the View menu to show the ones you need and hide the ones you do not, and the mouse to dock and undock them. The default configuration of plotting tools is shown below. MATLAB preferences remember the current arrangement of plot tools

— whether they are visible or not — each time you exit; if you want to revert to the default configuration you need to restore the arrangement shown below manually.

(34)

Property Editor displaying

lineseries properties Click to add

data to axes Click to display Property Inspector Figure Palette Axes subplots lineseries selected Figure Plot Browser

Managing Plotting Tools

Each of the plotting tools shown above can be docked or undocked from its figure, or dismissed by clicking thexat the right end of its titlebar. If you dismiss a tool and want it back again, you can raise it from the View menu or by typing one of several commands. For instance, if you had undocked, and then dismissed the figure palette, you could type either

plottools('on','figurepalette') or

(35)

figurepalette

Your desktop configuration might then look like this:

Figures Groups. When you activate any plot tool for a figure (or dock the figure in the desktop), the figure becomes part of a Figures group. Figures groups are desktop containers that you can dock in your desktop. Individual figures are not dockable except within the Figures group container. If you create subsequent figures, they will also dock in the Figures group, where they can be panelled or overlapped for viewing. A row of tabs appears along the bottom, one for each figure in the group.

When you dock a plot tool in a figure group and then dock the figure group in the desktop, the tool is included in that section of the desktop as well, as the following illustration shows:

(36)

Working with Multiple Figures. When you create a new figure and plot into it, It is created without plotting tools enabled, even if another figure already has them open:

figure plot(y1,w1)

This generates a freestanding plot, like this:

(37)

If you then open the plotting tools for the figure by clicking the Open Plotting Tools icon , the figure docks in the figure window:

(38)

The new figure might seem to disappear if the Figures window is hidden, but it will overlay the existing plot within that window (it does not replace it).

You can switch between the two figures by clicking the tabs at the bottom of the figure area. Be aware that clicking thexon the right side of a figure’s tab deletes that figure entirely, without asking for confirmation.

If you want to see both figures at once, use the Tiling Palette

at the upper right corner of the Figures window to arrange the figures. For example, clicking the Left/Right tiling tool lays out the two figures side by side:

(39)

As the above illustrations shows, plot objects can be selected in both figures, but only one figure has focus at a time.

The Figure Palette

The Figure Palette contains three panels. Select the panel you want to view by clicking the respective button, which twists down the panel and exposes its contents.

The Figure Palette enables you to perform the following tasks with these panels:

New Subplots— Add 2-D or 3-D axes to the figure.

Variables— Browse and plot workspace variables.

Annotations— Add annotations to graphs.

(40)

Adding Subplot Axes

TheNew Subplotspanel enables you to create a grid of either 2-D or 3-D axes. To display the selector, click the grid icon next to the axes type. A subplot selector grid appears.

As you move the cursor, squares darken to indicate the layout of axes that will be created if you release the mouse button. ClickCancelat the bottom of the grid to leave the figure unchanged.

The picture above shows theNew Subplotspanel selected to display four equally sized axes in the figure. Existing axes resize as required to accommodate the new layout.

Plotting Workspace Variables

TheVariablespanel displays current workspace variables. Double-clicking a variable in this panel opens that variable in the Variable Editor. If you select a variable and right-click to display the context menu, you can select a graphics function to plot the variable.

(41)

For example, the following picture illustrates how to plot the columns of variableZ. This is equivalent to passing a matrix to theplotfunction.

The context menu contains a list of possible plot types based on the variable you select and also enables you to perform certain operation on the variable, such as opening it in the Variable Editor, saving, copying, and so on.

The context menu items may change when you select different variables because a particular variable might be incompatible some of the plot types.

(42)

Drag and Drop Plotting

You can also drag the variable directly into an axes, in which case MATLAB selects the first appropriate plot type for that variable. If there are multiple axes, you must first select the one you want to plot in and then drag the variable to that axes.

In the previous example, the variableZ would be plotted using theplot function if you were to drag it into an axes.

If the desired plotting function is not available from the context menu, you can select thePlot Catalogitem to open the Plot Catalog tool. You can also use the Plot Selector from the Workspace Browser to create different types of plots of selected variables.

The Plot Catalog Tool

The Plot Catalog makes using MATLAB and toolbox plotting functions easy by eliminating the need to type their commands. You can select workspace variables or type their names (as well as MATLAB expressions) in the Plotted Variablestext field. The tool then passes the contents of the field as arguments to the plotting function you select. You can also enter function handles to pass to one of the “ez...” family of plotting functions from the Analytic Plotscategory.

(43)

The following picture shows the Plot Catalog tool with thepolargraphing function selected to display the expressiont,exp(-alpha*t).*sin(.5*t).

The Plot Catalog has three vertical panes. Use the left pane to select a category of plots. The middle pane shows thumbnail plots and descriptions of all functions in the category. Functions that MATLAB determines are unable to plot the data display in grey and cannot be chosen. The right pane presents the syntax description of the plotting function you choose from its function reference page. You see the entire reference page in the Help browser, click More Help... at the top right.

To generate a plot, click its icon on the middle pane, or clickPlotorPlot in New Figurebelow the right pane. The Plot Catalog assesses the arguments

(44)

you provide before calling the graphics function to determine if their number, class, and size match any of the function signatures. When it detects a mismatch, it displays diagnostics in the right pane to help you proceed.

Tip The Plot Catalog and Plot Selector can fail to diagnose some deficiencies that prevent a graphics function from being able to plot variables or

expressions. If no plot displays, or if a plot seems to be incorrect after you click for one, check the Command Window for warning and error messages.

The Plot Catalog has all the capabilities of the Plot Selector. To learn more about how you can use these tools, see “Creating Plots from the Workspace Browser”.

Adding Annotations to Graphs

TheAnnotationspanel enables you to insert annotation objects into a plot.

To add an object, first select the object you want to add, and then click and drag the mouse to position and size the object.

See “How to Annotate Graphs” on page 3-2 for more information about the various types of annotation objects.

The Plot Browser

The Plot Browser provides a legend of all the graphs in the figure. It lists each axes and the objects (lines, surfaces, etc.) used to create the graph.

For example, suppose you plot an 11-by-11 matrixz. Theplotfunction creates one line for each column inz.

plot(z,'DisplayName','z')

(45)

When you set theDisplayNameproperty, the Plot Browser indicates which line corresponds to which column.

(46)

If you want to set the properties of an individual line, double-click on the line in the Plot Browser. Its properties are displayed in the Property Editor, which opens on the bottom of the figure.

You can select a line in the graph, and the corresponding entry in the Plot Browser is highlighted, enabling you to see which column in the variable produced the line.

Controlling Object Visibility

The check box next to each item in the Plot Browser controls the object’s visibility. For example, suppose you want to plot only certain columns of data inz, perhaps the positive values. You can deselect the columns you do not want to display. The graph updates as you deselect each box and rescales the axes as required.

(47)

Deleting Objects

You can delete any selected item in the Plot Browser by selectingDelete from the right-click context menu.

Adding Data to Axes

The Plot Browser provides the mechanism by which you add data to axes.

The procedure is as follows:

(48)

1 Select a 2-D or 3-D axes from theNew Subplotssubpanel.

2 After creating the axes, select it in the Plot Browser panel to enable the Add Databutton at the bottom of the panel.

3 Click theAdd Databutton to display the Add Data to Axes dialog.

The Add Data to Axes dialog enables you to select a plot type and specify the workspace variables to pass to the plotting function. You can also specify a MATLAB expression, which is evaluated to produce the data to plot.

Selecting Workspace Variables to Create a Graph. Suppose you want to create a surface graph from three workspace variables defining theXData, YData, andZData(see thesurffunction for more information on this type of graph).

In the workspace you have defined three variables,x,y, andz. To create the graph, configure the Add Data to Axes dialog as shown in the following picture.

(49)

Using a MATLAB Expression to Create a Graph. The following picture shows the Add Data to Axes dialog specifying a workspace variablexfor the plot’s x data and a MATLAB expression (x.^2 + 3*x + 5) for the y data.

You can use the defaultX Datavalue ofindexif you do not want to specify x data. In this case, the y data versus the index of the y data values are plotted, which is equivalent to calling theplotcommand with only one argument.

The Property Editor

The Property Editor enables you to access a subset of the selected object’s properties. When no object is selected, the Property Editor displays the figure’s properties.

Ways to Display the Property Editor

There are a variety of ways to display the Property Editor:

Double-click an object when plot edit mode is enabled.

Select an object and right-click to display its context menu, then select Properties.

SelectProperty Editorfrom the Viewmenu.

Use thepropertyeditorcommand.

(50)

Changing Plot Types

You can use the property editor to change the type of plot used to display data. For example, you can change the following line graph to a stem, stairs, area, or bar graph by changing thePlot Typefield.

Accessing Object Properties with the Property Inspector

The Property Editor enables you to change the most commonly used object properties. To access more object properties, use the Property Inspector.

Open the Property Inspector by clicking theMore Propertiesbutton on the Property Editor or by typinginspectin the Command Window. The following picture shows the Property Inspector displaying the properties of the same lineseries object as that in the previous picture.

(51)

The default view of properties is an alphabetic list; you can change to a tree view by clicking the icon in the upper left corner containing plus marks. Click the “AZ” icon to its right to return to an alphabetized list view. Properties that contain fields, such as RGB color components, have a plus mark to their left you can click to expose the individual values. You can change properties that have enumerated values, such asMarker, via drop-down lists (downward-pointing triangles on the right edge of the inspector window). The

(52)

following figure shows theMarkerproperty being set todiamondusing the Property Inspector (note the tree view in which other groups of properties have been collapsed):

There are a few properties of objects that the Property Inspector does not show, for exampleParentandChildren. For complete descriptions of the properties of graphics objects, use the Handle Graphics Property Browser.

(53)

Getting Help for Object Properties

If you are not sure what a property does or what values it can take on, you can get a description of it from the Property Inspector. To do so, right-click on the name or values of a property and selectWhat’s Thisfrom the popup menu; a Help window opens displaying the property reference page for the current object, scrolled to the property you clicked. The following picture shows how this works:

(54)

Right-click properties to see their descriptions

Property description displays in context- sensitive help window

(55)

Accessing Objects You Cannot Click

If you want to access the properties of light oruicontextmenusobjects, you need to get the handle using MATLAB commands, because you cannot click on these objects.

For example, to get the handles of all light objects in the current axes, use findobj.

h = findobj(gca,'Type','light');

Then use theinspectcommand to display the Property Inspector.

inspect(h) % Inspect all light objects

inspect(h(1)) % Inspect the first light object in list

(56)

Example — Plotting Workspace Variables

In this section...

“Identifying Workspace Data to Plot” on page 1-36

“Adding a Subplot” on page 1-39

Identifying Workspace Data to Plot

This example illustrates how to use the plotting tools to graph a workspace variable versus an expression typed into theAdd Data to Axesdialog.

Create a variable in the workspace, x = -2*pi:pi/25:2*pi;

Use theplottoolscommand to create a figure group with the plotting tools attached.

plottools

Click2D Axesin theNew Subplotpanel of the Figure Palette.

(57)

Once the axes appears, theAdd Databutton on the Plot Browser is activated.

Click this button to display the Add Data to Axes dialog. When the Add Data to Axes dialog is displayed, enter the following values:

Select plot as thePlot Type.

SetX Data Sourcetox.

SetY Data Sourcetosin(x).^2.

ClickOKto plot this data.

(58)

A plot ofsin(x).^2versusxappears.

Now add another plot to the same axes. ClickAdd Dataagain and specify the data to plot:

SetX Data Sourcetox.

SetY Data Sourceset tosin(x).^8.

ClickOKto plot this data.

Select the last plot (the green line) and set thePlot Typein the Property Editor toStem. The plot should now look like the following picture.

(59)

Adding a Subplot

Add a second axes below the current axes using theNew Subplotspanel.

Click the right-facing arrowhead next to2D Axesand move the mouse to darken two squares, one on top of the other.

(60)

This creates a subplot axes below the existing axes. The existing axes resize so both fit in the figure.

In the inserted axes, select its entry in the Plot Browser and then clickAdd Data.

When the Add Data to Axes dialog is displayed, enter the following values:

SetX Data Sourcetox.

SetY Data Sourcetosin(x).^3.

ClickOKto plot this data.

Now add another plot overlaid on the first by clickingAdd Dataagain and specify the data to plot:

SetX Data Sourcetox.

(61)

SetY Data Sourcetosin(x).^9.

ClickOKto plot this data.

Select the plot labeledsin(x).^9under the second axes in the Plot Browser.

Set thePlot Typein the Property Editor toArea.

Setting Axis Limits

Adjust thex-axis in both axes using the Property Editor.

Select the first axes in the Plot Browser.

ChangeX Limitsto -7 and 7.

Repeat these steps for the second axes.

Adding Titles and Labels

Select the first axes in the Plot Browser and set the following properties in the Property Editor:

SetTitletoEven Powers.

SetX Label toX.

Click theY Axistab and setY LabeltoSine of X.

Select the second axes in the Plot Browser and set the following properties in the properties panel:

SetTitletoOdd Powers.

SetX Label toX.

On theY Axistab, setY labeltoSine of X. The Plot Browser now reflects the new axes names.

(62)

The following picture shows the result of these steps.

(63)

Select the text of they-axis label on the first axes (now labeledEven Powers in the Plot Browser) and click theMore Propertiesbutton on the Property Editor. Set theRotationproperty to0and reposition the text by hand.

To make more space for they-axis label, which is now in a horizontal position, select the axes and move it to the right with the mouse.

Repeat this process for the second axes (labeledOdd Powersin the Plot Browser).

The repositioned text label now looks like the following picture.

NoteYou can always undo your last change to the graph by selectingUndo from theEditmenu.

(64)

Example — Choosing a Graph Type

In this section...

“Selecting a Graph from the Plot Catalog” on page 1-44

“Plotting Expressions” on page 1-48

Selecting a Graph from the Plot Catalog

This example shows how to use the Figure Palette and the Plot Catalog to select a graph type for the data you want to plot. A surface graph is often a useful way to visualize a function of two variables. Use the following steps represent the example data as a surface graph (using thesurffunction).

1 Create three variables in your workspace (x, y,z) that represent a mathematical function evaluated over a specified domain (-2 to 2).:

% Generate the values for x and y [x,y] = meshgrid(-2:.2:2);

% Evaluate z as a function of x and y z = x.*exp(-x.^2-y.^2);

2 Create a figure and attach to it the Figure Palette:

figure;

figurepalette

3 Expand the figure paletteVariablespanel (unless it is already open).

4 Selectx, and theny, and thenzin theVariablespanel withshift+click to indicate the variables to pass the plotting function.

5 Open a context menu by right-clicking any of the three variables.

(65)

6 Since the function you want to use, surf, does not appear in the list, selectPlot Catalog.

The Plot Catalog tool opens in a new window.

7 In the Plot Catalog tool, select the3D Surfacesin the first column and surf(x,y,z)from the second column, as shown here.

(66)

8 Create the plot by clicking either thesurficon or thePlotbutton.

The following graph results.

(67)
(68)

Plotting Expressions

You can enter MATLAB expressions in the Plot Catalog tool. This example uses two variables to plot a line graph:

1 Create the following variables in the workspace.

t = 0:.01:20;

alpha =.055;

2 Plot a line graph oftversus this expression:

exp(-alpha*t).*sin(.5*t)

3 Create a figure and open the Figure Palette for it:

figure,figurepalette

4 Select the variabletand right-click to display the context menu. Select Plot Catalogfrom the menu.

(69)

5 When the Plot Catalog tool opens, add the expression to thePlotted Variablestext field so it readst,exp(-alpha*t).*sin(.5*t). You can reference the variablealphabecause you created it in the base workspace.

(70)

6 Click the thumbnail or thePlotbutton to create the graph. The figure looks like the following illustration.

(71)

The Plot Catalog tool issued the following commands, which appear in the Command Window:

plot(t,exp(-alpha*t).*sin(.5*t),'DisplayName',...

'exp(-alpha*t).*sin(.5*t) vs. t','XDataSource',...

't','YDataSource','exp(-alpha*t).*sin(.5*t)');

figure(gcf)

For information about working with variables in the MATLAB Workspace, see topics in MATLAB Workspace.

(72)

Example — Specifying a Data Source

In this section...

“Creating the Graph” on page 1-52

“Varying the Data Source” on page 1-52

“Data Sources for Multiobject Graphs” on page 1-54

Creating the Graph

First define two variables by issuing these statements in the command window.

t = 0:.01:20;

alpha =.055;

Next plottversus the expressionexp(-alpha*t).*sin(.5*t)using the plotfunction or the plot tools.

plot(t,exp(-alpha*t).*sin(.5*t))

Varying the Data Source

Plot objects have properties that enable you to specify the source of the data that defines the object. For example, you can specify a workspace variable name or a MATLAB expression as the value of theXDataSource, YDataSource, orZDataSourceproperty for a line in a plot (i.e., a lineseries object). You can then use the Property Editor to change the variable name or alter the expression, and the plot is updated to reflect the change.

After creating the graph, you can use the Property Editor to couple the plotted line to the MATLAB expression.

1 Double-click on the plotted line to display its property panel.

2 Enter the MATLAB expressionexp(-alpha*t).*cos(.5*t)in theY Data Sourcetext field.

(73)

You can now modify the expression in theY Data Sourcetext field and observe how the graph changes. After changing the text, click theRefresh Databutton to update the data.

In the following picture,alphais no longer negated, so the function grows instead of decays. Also the period has been shortened by changingsin(.5*t) tosin(1.5*t).

(74)

Data Sources for Multiobject Graphs

Suppose you create a line graph from matrix data. For example, z = peaks;

h = plot(z,'YDataSource','z');

Because there is one lineseries object for each column ofz, the following is true.

The data source forh(1)isz(:,1).

(75)

The data source forh(2)isz(:,2). ...

The data source forh(n)isz(:,n).

(76)

Example — Generating MATLAB Code to Reproduce a Graph

In this section...

“Create a Stem Plot and Generate Code for It” on page 1-56

“Data Arguments” on page 1-58

“Limitations” on page 1-58

Create a Stem Plot and Generate Code for It

Suppose you have created the following graph.

t = 0:.2:20;

alpha =.055;

stem(t,exp(-alpha*t).*sin(5*t))

Use the Property Editor to modify the graph. Select the stemseries and change the marker fill color to dark red, and marker edge color and line color to dark green. Remove the axes box, and change the font size for the axes labels to 8 to look like the following picture:

(77)

0 2 4 6 8 10 12 14 16 18 20

−1

−0.8

−0.6

−0.4

−0.2 0 0.2 0.4 0.6 0.8 1

You can generate code to reproduce this graph by selectingGenerate code from theFilemenu. MATLAB code generation software composes a function that recreates the graph and opens the generated file in the editor.

This feature is particularly useful for capturing property settings and other modifications you make using the plot tools GUI. The generated file appears in an editor window and consists of the following code:

function createfigure(X1, Y1)

%CREATEFIGURE(X1,Y1)

% X1: stem x

% Y1: stem y

(78)

% Auto-generated by MATLAB on 24-May-2006 14:23:45

% Create figure

figure1 = figure('Color',[1,1,1]);

% Create axes

axes('Parent',figure1,'FontSize',8);

hold('all');

% Create stem

stem(X1,Y1,'MarkerFaceColor',[0.8471,0,1608 0],...

'MarkerEdgeColor',[0.1686,0.5059,0.3373],...

'Color',[0,0.498,0]);

You must save the file before exiting MATLAB if you want to use it in future sessions.

Data Arguments

Generated functions do not store the data necessary to recreate the graph.

You must supply the data argumentstasX1andexp(-alpha*t).*sin(5*t) asY1to the function to recreate your graph. Of course, you can call the generated function with other argument pairs too.

Limitations

Attempting to generate code for graphs containing a large number of graphics objects (e.g., greater than 20 plotted lines) might be impractical.

(79)

Editing Plots

In this section...

“Why Edit Plots?” on page 1-59

“Interactive Plot Editing” on page 1-59

“Using Functions to Edit Graphs” on page 1-59

Why Edit Plots?

MATLAB graphs are formatted to provide readability, setting the scale of axes, including tick marks on the axes, and using color and line style to distinguish the plots in the graph. However, if you are creating presentation graphics, you might want to change this default formatting or add descriptive labels, titles, legends, and other annotations to help explain your data.

You can edit the plots you create two ways:

Using the mouse to select and edit objects interactively

Using MATLAB functions at the command line or in a MATLAB code file

Interactive Plot Editing

If you enable plot editing mode in the MATLAB figure window, you can perform point-and-click editing of your graph. In this mode, you can modify the appearance of a graphics object by double-clicking on the object and changing the values of its properties. You access the properties through a graphical user interface called the Property Editor.

For more information about interactive editing, see “Working in Plot Edit Mode” on page 1-61.

For information about editing object properties in plot editing mode, see “The Property Editor” on page 1-29.

Using Functions to Edit Graphs

If you prefer to work from the MATLAB command line or if you are creating a code file, you can use MATLAB commands to edit the graphs you create.

Referințe

DOCUMENTE SIMILARE

Abstract: The Canadian Immigration and Refugee Protection Act provides that one of the objectives of immigration is “to see that families are reunited in Canada.” The Act

Keywords: trickster discourse, meaning, blasphemy, social change, transgression of social norms.. The Myth of the trickster and its

The evolution to globalization has been facilitated and amplified by a series of factors: capitals movements arising from the need of covering the external

Using a case study designed for forecasting the educational process in the Petroleum-Gas University, the paper presents the steps that must be followed to realise a Delphi

The Black Sea seaside represents an important tourist and spa area not only for Romania, where there can be found the following resorts: Năvodari, Mamaia, Constanţa, Neptun,

We then go on to examine a number of prototype techniques proposed for engineering agent systems, including methodologies for agent-oriented analysis and design, formal

Key Words: American Christians, Christian Right, Christian Zionism, US-Israel Relations, Conservative Christians Theology, State of Israel, Jews, Millennial beliefs,

Un locuitor al oglinzii (An Inhabitant of the Mirror), prose, 1994; Fascinaţia ficţiunii sau despre retorica elipsei (On the Fascination of Fiction and the Rhetoric of Ellipsis),