MATLAB: An introduction Christian Ruff Course overview • Autumn 2007: Using MATLAB for data analysis and visualisation – – – – – Session 1: Introduction Session 2: Data types, handling data Session 3: Statistical data analysis and Visualisation Session 4: Producing graphs in MATLAB Session 5: User-defined Topics, questions and answers • Spring 2008: Using MATLAB to run experiments and collect data • April or May 2008: SPM – a MATLAB “toolbox” • Summer 2008: Advanced experimental and data analysis procedures Practical demonstration with code, not lecture All materials online at http://www.icn.ucl.ac.uk/courses/MATLAB-Tutorials/index.htm What is MATLAB? • “MATrix LABoratory”, a high-level programming language / computing environment • Developed in the late 70s for computer science students commands for linear algebra routines (matrix operations) • Now also contains ‘intuitive’ routines for graphing, user interfaces, hardware control • Today most popular environment in scientific computing (ahead of Mathematica, Octave, or Python); over 1.000.000 users in science, engineering, industry, etc. • Learning MATLAB is good for almost any scientist – Many specialised toolboxes available (e.g., statistics; neural network modelling; image processing, etc.) – A lot of code for specific scientific purposes freely available on the internet (e.g., COGENT, PSYCHTOOLBOX, SPM, ANSLAB) – Easy to modify existing routines / write your own code / learn how others solve problems How do I get started? • http://www.mathworks.com • Academic license is £128 for basic package; £128 for each toolbox (e.g., statistics); free trial version available • Windows, Mac, LINUX versions • Excellent documentation and tutorial (both online and in pdf form) at http://www.mathworks.com/support/ or by typing help or doc in MATLAB • Other MATLAB tutorials on the web: – http://www.icn.ucl.ac.uk/webspace/users/ahamilton/matlab.htm – http://www.mrc-cbu.cam.ac.uk/Imaging/Common/matlab.shtml – http://www.indiana.edu/~statmath/math/matlab/ • A good book: “Mastering Matlab” by Hanselmann and Littlefield • Lots of functions at http://www.mathworks.com/matlabcentral/ Demo: Structure and handling of MATLAB • • • • • • • • Starting MATLAB Command window Evaluating commands: comma and semicolon Variables: Scalars and matrices operators errors Help (F1) / doc cls • • • • Workspace Clear: variables and all load / save .mat files Current directory, pwd, ls, cd + graphical options for this • • • Command history; arrow up/down Editor: save series of commands as .m files, execute with F9 / button / run Dock editor Demo: Functions • Functions: function [output1,output2,…] = myfun(input1, input2,…) • Functions: – Input needs to be defined in command, output not – Function workspace: a separate module of the workspace • contains inputs to functions, transformation variables, and outputs from function • These variables are not visible from the global workspace • Functions can call other functions and contain subfunctions – Commands without semicolon are visible in command window – Graphical output is visible Demo: Functions in MATLAB • MATLAB – just a collection of functions: – Built-in functions cannot be edited but have an .m-file (for help) – Add-on functions are just an ordinary .m file that you can see/edit/add • How MATLAB finds a function: – – – – Embedded in the function as subfunction Built-in functions Local directory Path in descending order • Add functions to directory outside of the MATLAB toolbox (e.g., ‘work’ – otherwise MATLAB might overwrite them when re-installed • Dependency report Demo: Errors and debugging • Errors in functions: – Error message with pointer to problem (but workspace problem) • Help text and doc text • debugging mode: access the function workspace and go through code/functions step by step: – Stop if error – Breakpoints