An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell...
Transcript of An Introduction to MATLAB - UCLA · An Introduction to MATLAB Day 1 Simon Mitchell...
![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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/2.jpg)
∗ 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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/3.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/4.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/5.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/6.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/7.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/8.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/9.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/10.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/11.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/12.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/13.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/14.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/15.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/16.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/17.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/18.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/19.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/20.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/21.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/22.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/23.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/24.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/25.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/26.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/27.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/28.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/29.jpg)
∗ 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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/30.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/31.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/32.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/33.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/34.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/35.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/36.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/37.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/38.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/39.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/40.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/41.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/42.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/43.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/44.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022040611/5ed98dc11b54311e7967c72d/html5/thumbnails/45.jpg)
Combining What We’ve Learned