an introduction to matlab - ucla · an introduction to matlab day 1 simon mitchell...

45
An Introduction to MATLAB Day 1 Simon Mitchell [email protected]

Upload: others

Post on 31-May-2020

24 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

An Introduction to MATLABDay 1

Simon [email protected]

Page 2: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

∗ High level language∗ Programing language and development environment∗ Built-in development tools∗ Numerical manipulation∗ Plotting of functions and data∗ Implement algorithms∗ Create models and applications∗ Many built in functions∗ Interface with other languages∗ Create graphical interfaces

Page 3: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

The MATLAB Environment

Page 4: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Current Folder

Page 5: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Command Window >>

Page 6: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Workspace

Page 7: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Editor

Page 8: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Command Window Basics

Page 9: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Common Arithmetic Operators

+ Addition- Subtraction * Multiplication/ Division^ Exponential() Order operations

Page 10: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Semicolons in MATLAB

Suppress the output from a MATLAB expression

Page 11: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Comments %

Suppress the output from a MATLAB expression

Page 12: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Variables

Variable name = variable value

Be careful of i,j

Page 13: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Variables 2

MATLAB is CASE SENSITIVE

Page 14: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Close MATLAB

Reopen MATLAB

Page 15: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Initially Workspace is empty

Page 16: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Load your workspace

Page 17: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Variables to result of expression

If an expression is not stored as a variable it will be stores as ‘ans’

Page 18: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Multiple assignments and ‘who’

Page 19: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

whos

Page 20: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

format

‘format short’ to get back to normal

Page 21: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Numbers are actually 1x1 Matrices

Page 22: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Operations apply to matrices

Page 23: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Operations apply to matrices

Page 24: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Put . before an operator to make it

element-wise

disp(variable) displays the contents of a variable

Page 25: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

fprintf – print something to command window

%s Format as a string.%d Format as an integer.%f Format as a floating point value.%e Format as a floating point value in scientific notation.%g Format in the most compact form: %f or %e.\n Insert a new line in the output string.\t Insert a tab in the output string.

Page 26: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Concatenating matrices

Page 27: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Concatenating matrices

Why is this 1,1?

Page 28: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Many Functions work on columns

Page 29: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

∗ 3 Ways of running code∗ Command Window∗ Scripts∗ Functions

Working with m-files

m-files

Page 30: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

New Script

Page 31: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Our First Script

Page 32: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Scripts can create variables in the workspace

Page 33: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Data Types

∗ Type declarations are not necessary in MATLAB

∗ MATLAB automatically decides data type

Page 34: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Data Types

single - single precision numerical datadouble - double precision numerical datalogical - logical values of 1 or 0, represent true and false respectivelychar - character data (strings are stored as vector of characters)cell array - array of indexed cells, each capable of storing an array of a different dimension and data typestructure - named fields capable of storing an array of a different dimension and data typefunction handle - pointer to a functionuser classes - objects constructed from a user-defined classInt8 uint8 int16 uint16 int32 uint32 int64 uint64 – don’t worry about these

Page 35: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Data Types

Page 36: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Testing Data Types

Page 37: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Relational Operators

< Less than>= Less than or equal to> Greater than>= Greater than or equal to== Equal to~= Not equal to

Useful for if statements!

Page 38: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

If Statement

Never run

Page 39: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

If else Statement

Page 40: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

If elseif Statement

Page 41: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Nested if Statement

Page 42: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

For loops

Page 43: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Combining What We’ve Learned

Write a script that takes a list of gene names and gene expression values and outputs only those gene names over a threshold.

PSUEDO CODE:For each gene in a list:

if its expression value is over the thresholdprint the gene name and the expression value

Page 44: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Combining What We’ve Learned

PSUEDO CODE:For each gene in a list:

if its expression value is over the thresholdprint the gene name and the expression value

Page 45: An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu ... ∗ Scripts ∗ Functions Working with m-files m-files. New Script

Combining What We’ve Learned