Presentation is loading. Please wait.

Presentation is loading. Please wait.

Matlab tutorial course Lesson 1: The Basics: variables, scripts and functions Dr Michael Berks

Similar presentations


Presentation on theme: "Matlab tutorial course Lesson 1: The Basics: variables, scripts and functions Dr Michael Berks"— Presentation transcript:

1 Matlab tutorial course Lesson 1: The Basics: variables, scripts and functions Dr Michael Berks

2 Why do I need to learn ‘it’? What is Matlab? Because Dr Graham said so…

3 What is Matlab? What does computer programming mean? What is a programming language? What is code? What is a computer program?

4 Something you use to achieve some task on a computer… Word Write a letter, essay, lab report etc… Excel Analyse results, produce graphs for your report etc… Angry Birds Kill time when you’re sitting in boring Matlab lectures…

5 What is a computer program? In medical imaging… Image(s) Lab results Patient data DATA Do something clever… PROGRAM Hopefully clinically useful! RESULT

6 What is a computer program? In medical imaging… DATA Measure volume of grey/white matter… PROGRAM Predict whether the patient has Alzheimer’s Disease RESULT MR images of the brain

7 What is a computer program? In medical imaging… DATA Measure amount dense breast tissue… PROGRAM Predict risk of developing breast cancer RESULT Mammogram acquired during routine screening + patient data

8 What is a computer program? In medical imaging… DATA Measure change in size of tumour… PROGRAM Tell doctor/drugs company if the drug is working RESULT MRI liver tumours before and after drug treatment

9 What does computer programming mean? Writing your own computer programs... … telling the computer to do exactly what you want. What is a programming language? A way of translating human logic into commands a computer understands… … like human languages, there a lots of different languages (often similar to each other), each with a specific set of rules (syntax, grammar) to obey. What is ‘code’? A chunk of commands in a specific programming language… A program consists of bits of code put together in a logical way … … by using other people’s code, you can incorporate bits of their program in your own (as long as you’re using the same language!).

10 Why do I need to learn how to write my own computer code? Think of it like learning to cook….. Now the big question….

11 You’re hungry and want something good to eat….. Get mum to cook Go to a restaurant Get a microwave meal x x x You live in Manchester Now! You’re student, you can’t afford it! You’re a fussy eater!

12 Vs Heating someone else’s food in the microwave Ok if they’ve cooked exactly what you want Cooking your own food Can eat exactly as you like it Vs Using a computer program Ok if it does exactly what you want Writing your own computer program Make it do exactly what you want Research is likely to be here

13 How good a cook/programmer do I need to be? Do I need to write all my programs from scratch? No! Just like in cooking, you don’t need make everything from raw ingredients, can use pre-made pasta, sauces, wine etc… Remember you can use other people’s code to include bits of their programs in yours … … but you do need to know the basics to put those bits together (how to chop an onion, when to add seasoning etc.) Vs

14 And finally…. What is Matlab? Matlab is both a program and a programming language... … it has lots of tools you can just use to analyse data and images. But you can also write code to extend it to do any analysis you like (although unlike a ‘pure’ language, it will only work if the Matlab program is installed on the computer). ExcelPhotoshop Writing your own code Using other people’s code +++ = A really powerful tool for imaging research!

15 Course aims To show you how to use Matlab To introduce some basic programming ideas common to all programming languages To give examples of some tasks in medical imaging processing To provide code you can reuse later in your MSc To make some pretty pictures!

16 First task Log on using your university ID Open a windows explorer window – Click on ‘Documents’, then ‘MATLAB’ – Make a new folder TutorialSlides Open an internet explorer/Google chrome window Go to the following webpage: – – Click on the Matlab Tutorial tab – Bookmark this page for future weeks

17 First task Save the week 1 powerpoint slides and tutorial exercise into your new TutorialSlides Leave the window open (we’ll need it later) Open the ‘lesson 1’ powerpoint file you’ve just saved – As I’m going through the slides, follow them on your computer Start a new Matlab session

18 Lesson Overview Using the Matlab interface Some simple commands in the Command Window Assigning variables and suppressing visual output Collecting sequences of commands: scripts An introduction to functions Basic programming tips: expressive variable names and comments

19 Command window

20 A fancy scientific calculator – 2+3 – 2*3 – 2/3 – 2^3 – 2^0.5 – 2+3*6/2 – (2+3)*6/2 Try using ↑ and ↓ – Select earlier expressions and edit them Any of the text in navy blue is code you can run in the command window. Try copy and pasting, then hitting return

21 Assigning Variables Use ‘=‘ to assign variables – Keeps data in memory to use again – a = 4 – b = – y = (4*a^2 +3*b^3) / 5 Can also self-assign: – b = b*2 Check the work space

22 Workspace Shows you what variables are currently stored in memory

23 Suppressing visual output Try a = rand(100) This creates a 100 x 100 matrix of random numbers Use clc to clear the Command Window Try a = rand(100);

24 Scripts Use clear to wipe the current memory – Check that the workspace is now empty Click the ‘New script’ button (top left of main menu) – Opens a blank page in the editor Copy the previous commands from the Command History Paste them into the blank document Save the document as ‘my_first_script.m’ Type my_first_script (note the lack of ‘.m’) at the command line

25 Scripts (cont.) Scripts allow us to run sequences of commands All data is stored in the main workspace, as if we typed the commands in the command window We can run parts of scripts by – Selecting text and hitting F9 – Using % to create ‘cells’ Even when ‘hacking around’ use scripts, date tagged (e.g. work2013_02_11) to run commands – That way you have a record of your work – Think of them as your Matlab lab book

26 Functions Scripts are useful… … but what if we want to re-compute y for different values of a and b? Create a new ‘script’, in the first line write – function y = my_fun(a, b) – y = (4*a^2 +3*b^3) / 5; Click save – Note how my_fun.m is automatically selected as the filename

27 Functions (cont.) Type y = my_fun(a, b); at the command line Clear the memory Try y = my_fun(4, 8); Note how y appears in the workspace, but a and b don’t Functions have their own memory space a and b are ‘arguments’ to the function – They allow data to be moved into the function’s memory y is the function’s ‘output’ – This allows us to get data from the memory space

28 Functions (cont.) Functions can be combined with other operators (and themselves) in commands – y1 = 2*my_fun(4, 8) + my_fun(3, 2) ; Functions can be called from other functions – function y = my_fun2(a, b) – y = my_fun(a,b) + my_fun(2*a, b/2); Matlab has 1000s of existing functions By combining these with your own functions you can get Matlab to do just about anything you want

29 How does Matlab work? Matlab interprets each command it sees It recognises certain keywords, mathematical operators etc. When it sees something that isn’t a keyword it 1.Checks if it is a variable in the current memory space 2.Looks for a script or function in the Matlab path 3.If it can’t find either, returns an error

30 The Matlab path Type ‘path’ at the command line This displays all the folders on your computer where Matlab will ‘look’ for functions and scripts Use ‘File -> Set path’ to add new folders If 2 functions/scripts have the same name, Matlab uses the first one it finds on the path – Avoid name clashes with existing functions – Avoid mixing variable and function names

31 Variable, script and function names Must start with a letter Followed by any number of letters, digits, or underscores. Matlab is case sensitive – A and a, my_fun and My_fun, etc are not the same name Certain keywords cannot be used – if, for, end, etc Be expressive: try and use names that – Describe what functions do – Describe what variables are

32 Organising your functions and scripts Use scripts to generate results/output for specific tasks – Assignments in your maths course – Experiments in your project – Give them a sensible name, and add comments at the start describing what they do Use functions for methods that can be reused across multiple tasks – Organise them in sub-folders E.g. ‘stats’, ‘optimisation’, ‘image_processing’ Remember, you can always rearrange the file structure – As long as you remember to add any new folders to the path

33 Functions A little demonstration… If we can understand this concept now, it will make life a lot easier later…

34 Functions (cont.) Type y = my_fun(a, b); at the command line Clear the memory Try y = my_fun(4, 8); Note how y appears in the workspace, but a and b don’t Functions have their own memory space a and b are ‘inputs’ to the function – They allow data to be moved into the function’s memory y is the function’s ‘output’ – This allows us to get data from the memory space


Download ppt "Matlab tutorial course Lesson 1: The Basics: variables, scripts and functions Dr Michael Berks"

Similar presentations


Ads by Google