Leanne Kuwahara-Week 10
From OpenWetWare
Jump to navigationJump to search
Purpose
To learn how to use matlab and create files MyFirstScript, LetsLoop and PlotTime.
Notes
Chapter 1
- 'clc' to clear screen
- matlab returns 'ans = 1' when no variable is entered
- create own variable: 'myvar = x'
- ';' suppresses output
- types of variables:
- scalar = x
- vector = [x y z]
- column vector = [x; y; z]
- matrix = [x y z; a b c; q r s]
- to extract from matrix:
- matrix(row, col)
- for whole row/col:
- matrix(row,:)
- for subset of matrix:
- matrix(row,1:2)
- variables shown in work-space
- clear from work-space 'clear myvar' (or 'all' if want all variables to delete)
- can see command history 'layout >> command history'
- double click command to run again
- click and drag to edit
- can format 'long' or 'short' to change number of decimal places shown
- types of variable classes:
- double
- single
- Boolean (logical): T/F
- cell array: similar to a matrix, but can include multiple data types
- mycell = {[1 2 3] [4 5 6 7]; 'v1' 'v2'}
- row 1: vector of doubles
- row 2L vector of strings
- access similar to matrix, but with '{}'
- mycell = {[1 2 3] [4 5 6 7]; 'v1' 'v2'}
Chapter 2
- script: series of text files the matlab runs sequentially
- comment: '%'
- can highlight, right click, 'comment' to comment out a large chunk of text
- send text ot command line: 'disp('string')'
- random integers: 'randi(row,col)'
- transpose: swap rows and cols in matrix
- Transpose = MatrixVar'
- hit 'run' to get commands to appear in window
- Transpose = MatrixVar'
- make matrices of all zeros/ones
- myZeros = zeros/ones(rows,cols)
- can right click on functions for help/explanation
- matrix multiplication/div vs. element multiplication/div
- matrix multiplication/div: M1*M2
- element multiplication/div: M1.*M2
- If statements
- = input(‘please enter a number: ’)—for keyboard input
- if: first condition
- disp(“negative”)
- elseif second condition
- disp(“perfect”)
- else—"catch all”
- disp(“too big”)
- end
- if: first condition
- switch statements: to implement patterns, or “clean-up” if statements
- get two input values (n, m) and type of calculation
- switch action
- case ‘add’
- output = n + m
- case ‘subtract’
- output = n-m
- otherwise
- disp(“sorry, no can do.”)
- end
- case ‘add’
- for loops
- need to set up index as vector
- for i = 1:10
- disp(i)
- end
- for i = 1:10
- to move in steps of 2: for i = 1:2:10
- can set step size and end number before hand as variables
- need to set up index as vector
- While loop: continues to run when while a certain flag is true, once flag is false, while loop stops
- be careful of infinite loops
- good to test threshold values
- set threshold variable
- ’break’ exits threshold
Chapter 3
- make sure function is in the matlab path
- use = csvread(‘fliename.csv’); to import external data
- can “debug” or check script at breakpoints (dashes near numbers)
- ’tictoc’ function tells how long it takes to run a function
- can pre-allocate time when working with large datasets
Chapter 4
- making plots
- plot(data)
- can turn on grid (‘grid on’)
- can adjust axis
- ’axis tight’
- ylim([min,max])
- can change color (y, m, c, b) of plot and type of marker by using a ‘string’ command after data
- can add annotations to plot
- xlabel(‘x-axis’)
- ylabel(‘y-axis’)
- title(‘title’)
- legend(‘legend1’,legend2’)
- can input images
- myIm = imread(‘filename.jpg’)
- can plot in 3D
- use surf function
- can add color bar (‘colorbar’) showing max and min values
- be careful of punctuation marks, spaces, and capitalizations!!!
Files
MATLAB exercise files: myFirstScript, LetsLoop, PlotTime
Acknowledgements
- Assigned Homework Partner: Alison King
- Except for what is noted above, this individual journal entry was completed by me and not copied from another source.
References
- Dahlquist, K. & Fitpatrick, B. (2019). "BIOL388/S19: Week 10" Biomathematical Modeling, Loyola Marymount University. Accessed from:Week 10 Assignment Page
- Moser, S. (2018). Learning MATLAB. 'Lynda Learning:Linkedin.' Accessed From: https://www.lynda.com/MATLAB-tutorials/Learning-MATLAB/688521-2.html
Links
- Assignment Pages:
- Individual Assignment Link:
- Week 1- Create User Page: Leanne K. Kuwahara
- Leanne Kuwahara-Week 2
- Leanne Kuwahara-Week 3
- Leanne Kuwahara-Week 4/5
- Leanne Kuwahara-Week 6
- Leanne Kuwahara-Week 7
- No week 8 assignment
- Leanne Kuwahara-Week 9
- Leanne Kuwahara-Week 10
- Leanne Kuwahara-Week 11
- Leanne Kuwahara-Week 12
- No week 13 assignment
- Leanne Kuwahara-Week 14/15
- Class Journal Page:
- Class Journal-Week 1
- Class Journal-Week 2
- Class Journal-Week 3
- Week 4 shared journal assignment extended to week 5
- Class Journal-Week 5
- Class Journal-Week 6
- Class Journal-Week 7
- No week 8 assignment
- Class Journal-Week 9
- Class Journal-Week 10
- Class Journal-Week 11
- Class Journal-Week 12
- No week 13 assignment
- Class Journal-Week 14/15
- BIOL388 Home Page: BIOL388
- User page: Leanne K. Kuwahara