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 '{}'

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
  • 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
  • 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
  • for loops
    • need to set up index as vector
      • for i = 1:10
        • disp(i)
      • end
    • to move in steps of 2: for i = 1:2:10
      • can set step size and end number before hand as variables
  • 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

Except for what is noted above, this individual journal entry was completed by me and not copied from another source.

References

Links