Presentation is loading. Please wait.

Presentation is loading. Please wait.

Getting started with Matlab: Outline

Similar presentations


Presentation on theme: "Getting started with Matlab: Outline"— Presentation transcript:

1 Getting started with Matlab: Outline
What, where and why of matlab. The matlab desktop and you Entering commands Variables and data types Plotting 101 Saving and loading data A “real-world” example

2 What is matlab? Scientific programming language
Optimized for working with matrices Interpolated language = immediate gratification Learn it in a second, master it in a lifetime Excellent visualization tools

3 Why NOT to use matlab? To do a single ANOVA!
For applications that absolutely must be super-fast, well written C/Fortran would be quicker (though my C/Fortran would not be). Low budget. Imitations exist, but usually lack some functionality (octave, scilab, R). Parallel programming, though some attempts exist to make matlab parallel and some imitations can do parallel programming (scilab).

4 Why use matlab? Modeling, data analysis, image manipulation
Excellent at manipulating large amounts of data with little effort. Plotting! Decent set of built-in functions: interpolation, linear algebra, time series, etc. Lots of free and not so free toolboxes. Mature language: GUIs, object-oriented programming

5 Where do you get matlab? University has a site license. Details available at my.ucdavis.edu software channel (contact Tom Fortis and Paul Hirose in EACS) Advantage: All the toolboxes Disadvantage: All the toolboxes; generally requires a network connection; have to pay $100-$200 / year Student version at bookstore for $100 Advantage: For life Disadvantage: Toolboxes extra, though cheap for students through Mathworks

6 The matlab desktop and you...
Current working directory Directory listing List of variables in workspace Enter commands here Command history

7 Entering commands First, change working directory to your student directory. Commands are entered directly in command window. 1 + 2 * 10 A = pi B = 2; C = A + B Note the difference between the second and third commands – semi-colon at end silences output. Use Workspace tab or whos command to see defined variables.

8 Getting help Most matlab functions have excellent and easy to access documentation. help help sqrt helpdesk Use lookfor command as simple search. lookfor anova Other useful commands for looking for help: edit, type, which, more

9 Variables and data types
Matlab figures out variable type all by itself A = 1 b = A > 1 % This is a boolean c = 'David' d = [ 1 2; 3 4 ] The lines, in order, define a number, boolean, string and array. Comments start with % % This does nothing Get rid of variables with clear clear c whos There are other useful data types in matlab, such as cells and structures (future classes).

10 Array Basics All numbers in matlab are really arrays
d = [ 1 2; 3 4; 5 6 ] size(A) size(d) Getting at array elements: f = d(1,2) g = d(:,1) % : used to get a col or row h = d(2:end,:) % note use of “end” Creating a list of numbers: A = 1:5 C = 10:-2:1 Array element-by-element math: B = A.*A C = A.^2

11 Plotting 101 Matlab has extremely powerful plotting capabilities
x = -pi:0.1:pi; y = sin(x); plot( x, y, 'r' ) % r = red Add another curve with hold hold on plot( x, cos(x).^2, 'b' ) hold off Adding labels title( 'Wow!' ) xlabel( 'Time (yr s^{-1})' ) ylabel( 'US Economy' ) Try this: z = cos(x); plot( y, z, 'r--', y+1, z, 'b-' )

12 Saving and loading data
The save and load commands do just that: a = 1; b = 45; save myvars.mat a b clear a b load myvars.mat a .mat is a binary format that matlab saves data in. Matlab loads simple text files also: edit z:\Pkg\WFCB\class1\moms.txt %justlook load z:\Pkg\WFCB\class1\moms.txt compass( -moms(:,end-1), -moms(:,end) ) save moms2.txt moms -ASCII -ASCII tells it to save as a text file. compass plots arrows (winds in this case).

13 Simple “real world” example
That MOMS data was temperature and wind from BML load z:\Pkg\WFCB\class1\moms.txt moms(1:4,:) % Take a look at the data Make a temperature and alongshore wind plot plotyy(moms(:,4),moms(:,5), ... moms(:,4),moms(:,7)) Note how I used “...” to continue command on two lines.

14 Exercises with MOMS data
Exercise1: What is the mean temperature for time period? (hint: Use lookfor to find function to calculate mean) Exercise2: Calculate correlation between temperature and alongshore wind. Exercise3: What about lagged correlations? (This is harder, don't worry if you can't do it.)

15 Solution to exercises The solution can be found in class directory: class1_solutions.m edit ... z:\Pkg\WFCB\class1\class1_solutions.m addpath z:\Pkg\WFCB\class1 class1_solutions % Click on first figure to see second. The addpath command tells matlab to look for functions and scripts in that directory as well.


Download ppt "Getting started with Matlab: Outline"

Similar presentations


Ads by Google