lecture 3 - stanford university · 2019-02-10 · lecture 3. over 600 rhode islands could fit...
TRANSCRIPT
![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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/16.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/21.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/25.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/26.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/27.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042305/5ed0c8a712139c4ae153a779/html5/thumbnails/28.jpg)
Scatter Plots
28
Varying marker color.
color
size