28
CME 192: Introduction to Matlab Lecture 3

Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

  • Upload
    others

  • View
    17

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

CME 192: Introduction to Matlab

Lecture 3

Page 2: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Over 600 Rhode Islands could fit inside Alaska

Page 3: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Lecture 2 Topics• Scripts and functions

• Control flow

• Debugging

Lecture 3 Topics• Data Structures

• Cell arrays

• Struct

• Plotting

Page 4: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Data Structs

4

• A structure array is a data type that groups related data using data containers called fields. Each field can containany type of data.

• Access data in a structure using dot notation of the form structName.fieldName.

Page 5: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Data Struct Applications

• Very useful for organizing big sets of data.

• Saving time series and analysis on it.

• Common way of saving research data.

5

Page 6: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Struct Example

6

• Struct name is ‘patient’

• Field names are ‘name’, ‘billing’, and ‘test’.

Page 7: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Creating Data Structs

7

Struct name Alternating field name and field value

The fields of the struct

Page 8: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Appending to Structs

8

We perform the same process but we add a second

element to our struct.

The same field names must be used. In the variable editor each

row is a different fruit.

Page 9: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Creating a Large Array of Struct

9

Each field name is given and the respective values are

given in “{ }”.

Page 10: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Copying and Modifying a Struct

10

Makes a hard copy of the struct.

Changes to one struct will not be

updated in the other.

Updates can be made

Page 11: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Plotting in Matlab

• When using a Matlab plot function a new window will

automatically come up.

• You can define new figure windows by using the

command ‘figure’

• Figures can be numbered such as figure(1); figure(2); figure(3);

11

Page 12: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Plotting Options

• Matlab will plot over whichever figure is open by

default. To plot a second graph over that one use

‘hold on’.

• ‘xlablel’, ‘ylabel’, and ‘zlabel’ are all used to label the

axes.

• A legend can be added by using ‘legend’ and listing

all the domains.

12

Page 13: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Types of Plots

• Bar

• Line

• Scatter

• 3D

13

Page 14: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Bar Plot

14

Creates the bar plot and plots the values from the designated fields.

• Creates the labels for the plot.

• gca stands for “get current axes.”

Page 15: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Plot Output

15

Page 16: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Stacking the Bars

16

Create new variable

Stack the fields

Page 17: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Stacking the Bars

17

Page 18: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

3D Bar Graph

18

We now use the function ‘bar3’

• The x, y, and z axes are labeled.

• In y we give the fruit names.

• In x we give the calorie type.

• In z we give the grams.

Page 19: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

3D Bar Graph

19

Page 20: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Plotting Additional Data

20

Page 21: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Plotting Additional Data

21

Page 22: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Plotting Numerical Functions

22

https://www.mathworks.com/help/matlab/ref/plot.html

Page 23: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Plotting Options

23

Markers and line parameters

can be modified.

Page 24: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Plotting Options

24

https://www.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.line-properties.html

https://www.mathworks.com/help/matlab/ref/plot.html

Page 25: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Another Way of Viewing Data?

25

Line plots may not always be the best

method for viewing data

Page 26: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Scatter Plot

26

Places data points but does

not connect them.

Page 27: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Scatter Plots

27

Varying marker color.

color

size

Page 28: Lecture 3 - Stanford University · 2019-02-10 · Lecture 3. Over 600 Rhode Islands could fit inside Alaska. Lecture 2 Topics • Scripts and functions • Control flow • Debugging

Scatter Plots

28

Varying marker color.

color

size