1. Basics Working environment Dealing with matrices Useful functions Logical operators Saving and loading Data management Exercises 2. Programming Basics graphics settings - ex Functions & scripts Vectorization Exercises 3. Applications Generic scripts Means Anomalies STD Correlations Time series: SOI SSTA detrending filtering Interpolation EOFs REOFs CEOFs Spectral analysis Wavelet day 1 day 2 days 3, 4 & 5 Tutorial Program B.Aires, 20-24/02/06 Centro de Investigaciones del Mar y la Atmosfera and Department of Atmospheric and Oceanic Sciences 1 (UBA) E.Scoccimarro, A.F.Carril
edit the exe-5 graphic settings : Programming script functions 2
functions & scripts : MATLAB & Practical Applications Programming Matlab allows commands to be executed in two ways: Interactive You can type commands directly to Matlab, allowing you to proceed through a computation step-by-step, inspecting variables, and plotting intermediate results. Program By creating a file of commands, a sequence of commands can be stored and executed as if each were typed in interactive mode. The file, created in an ordinary editor or by Matlab, should be named.m. It is executed by typing its name without the '.m'. A Program can be a script or a function. 3
.. simple script : MATLAB & Practical Applications Programming >> edit simple_script.m 4
functions & scripts : MATLAB & Practical Applications Programming A function is a script that contains function on the first line function [avgy] = runavg ( y, len) A function can be called from command line, >> avgy = runavg (y, len); FUNCTION output inputs or by using others functions or scripts. 5
function call: MATLAB & Practical Applications Programming No memory of the internal variables, used by the function. 6
Programming functions with dynamic number of inputs: How to use varargin. 7
Programming Built-in functions: Functions that are frequently used or that can take more time to execute are often implemented as executable files. These functions are called built-ins. Unlike M-file functions, you cannot see the source code for built-ins. As with M-file functions, you can identify which functions are built-ins using the exist function. This function identifies built-ins by returning the number 5: >> exist max ans = 5 Or trying to edit it: for built-in function you will get only help info. 8
Indexing Subscripted Indexing: Linear Indexing: A C B rows column D 9
Indexing Two new functions: IND2SUB: SUB2IND: Multiple subscripts from linear index. Linear index from multiple subscripts. >> [ i, j, k ] = ind2sub ( size (A), 16 ) i = 1 j = 2 k = 2 >> ind = sub2ind ( size (A), i, j, k ) ind = 16 10
Indexing Logical Indexing: E With logical, or Boolean, indexing, the index parameter is a logical matrix that is the same size as A and contains only 0's and 1's. The elements of A that are selected have a '1' in the corresponding position of the logical indexing matrix. 11
Indexing Logical Indexing example: How to use logical indexing power of MATLAB to remove negative values in a series. 12
Vectorization Let s talk about things that can be done in parallel.. 13
Vectorization Matrix Functions of Two Vectors: To evaluate a function F of two variables: F(x,y) = x * exp ( -x 2 y 2 ) for yi=1:16 for xi=1:21 f(yi,xi) = x(xi).* exp (-x(xi).^2 - y(yi).^2); end end 14
Exercises Exe 5: Read nino3 SSTA series in binary format, plot and save the image. DONE Exe 6: Vectorizing loops: 6a. vectorizing a double FOR loop. 6b. vectorizing code that finds the cumulative sum of a vector every fifth element. (need cumsum function) 6c. Create a code that repeats a vector value when the following value is 0. Exe 7: Read an SST field and plot global mapping in different projections. (starting from the Exe 3 [day 1]) Exe 8: Create Scatter Plots. Some info about subplotting and page layout settings. B.Aires, 20-24/02/06 Centro de Investigaciones del Mar y la Atmosfera and Department of Atmospheric and Oceanic Sciences (UBA) E.Scoccimarro, A.F.Carril 15