MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB...

Post on 28-May-2020

16 views 0 download

Transcript of MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB...

Introduction to MATLAB

What you will learn• The most important commands (from my view)

• Writing scripts (.m files)• Defining MATLAB variables• Matrix/array operations• Importing and extracting data• 2D plots• Intro to statistic toolbox

• Command window: enter the command/instruction• Current folder: directory where we save our work (click “browse” button if you want to 

change)• Workspace: Where our data is stored in MATLAB during the current session (will be removed 

if we close our current session)• Command history: record commands entered in the command window

Scripts• Script: A sequence of commands• Use % to add comments for explanation/hints• Save as an .m file (save before we run it, no space in the file name)

• Demo: Display the text ‘finding nemo’ (use the command ‘disp’ to display strings)

• Demo: x=5; y=x^3; z=x+y; calculate z

• What we do if we are stuck:• Use “help”! (The blue question mark on the top row)

• Use the command “doc” to get detailed information about the command.

Variables• Variables are places to store data• To create a variable, simply assign a value (on the right) to a name 

(on the left):• A=3.14• B=‘nemo’• First character of a variable’s name must be a LETTER; after that, 

any combination of letters, numbers and _• CASE SENSITIVE! (Nemo is different from nemo) Some names for variables are reserved:

Do it together• Write a script with three MATLAB variables• a=3.14;• b='nemo';• c=5*2+1• Save the script and the variables• Script will show up in the current folder as .m; variables as .mat

• How about do the following commands:• 1+1=a,• a=b=1

Vectors as MATLAB variables 

x=1:1:5y=linspace(1,5,5)

Use “;” to suppress output line (save space!) Imagine if you are writing x=1:0.1:1000, what would happen if you don’t put the “;”

Matrices as MATLAB variables 

Let’s create a 2*3 matrix:1 3 52 4 6 

You can also use “:” to do the matrix above

Matrix operation  vs.   Array operation• Matrix operation (standard 

mathematical way):• Inner dimension must 

agree (number of columns of A=number of rows in B)

• Array operation: element‐wise• Two matrices: same shape and size• Use dot (.) before the operator• .*  ./   .^

Let’s try two types operationsFirst, create matrices A and B.

Creating variables by importing external data (e.g., text file, spreadsheet)

• Use Import Wizard• 1. Select file to be imported in the current folder. Right click on it and select ‘import data’.

• 2. If we do that correctly, it will show up in the workspace window.

Instead of using Import Wizard, you can also import data programmatically by using the command “import”. Use ‘doc’ for details

Do the “cherry picking”• Extract data:1. Double click on the variable (large dataset) in the workspace window2. Highlight the area to extract3. Right click and select ‘create variable from selection’4. Rename the new variable5. Right click on the variable on the workspace and save it to the current folder • Example: Seattle and Houston rainfall data

Plot• Method 1: use command “plot” • Use ‘doc plot’ to see the syntax of plot• Method 2: use interactive plotting tool. Hold control and select the variables in work space, then click on the “plot” button

• Then use “plottools” command for plot editing

Statistics toolbox• Mean and standard deviation command• m   = mean(X); s = std(X)• pH in Lake Washington example• Use “cdfplot” to display the plot of the cumulative distribution function (cdf) for the data

• Use “dfittool” to fit the data

• Enjoy your homework