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

Slides:



Advertisements
Similar presentations
Programming for Beginners
Advertisements

Introduction to Applications & Basic Features. What is MATLAB®? MATLAB® /Simulink® is a powerful software tool for: Performing mathematical computations.
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Tutorial 8: Developing an Excel Application
Tutorial 12: Enhancing Excel with Visual Basic for Applications
CIS101 Introduction to Computing Week 05. Agenda Your questions CIS101 Survey Introduction to the Internet & HTML Online HTML Resources Using the HTML.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Using the Visual Basic Editor Visual Basic for Applications 1.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Welcome to the Exciting World of ! Lessons to familiarize yourself with.
CIS101 Introduction to Computing Week 09 Spring 2004.
SPSS 1: An Introduction to the Statistical Package SPSS Suzie Cro MRC Clinical Trials Unit.
Introduction to VBA. This is not Introduction to Excel We’re going to assume you have a basic level of familiarity with Excel If you don’t, or you need.
Basic HTML The Magic Of Web Pages. Create an HTML folder  Make a folder in your H drive and name it “HTML”. We will save EVERYTHING for this unit here.
Recitation 1 Programming for Engineers in Python.
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Digital Image Processing Lecture3: Introduction to MATLAB.
 Using Microsoft Expression Web you can: › Create Web pages and Web sites › Set what you site will look like as you design it › Add text, images, multimedia.
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
A First Program Using C#
Matlab tutorial course Lesson 2: Arrays and data types
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
Amber Annett David Bell October 13 th, What will happen What is this business about personal web pages? Designated location of your own web page.
Lecture 4 MATLAB Windows Arithmetic Operators Maintenance Functions
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Lesson 1 Introduction.
Microsoft ® Office OneNote ® 2003 Training Get to know OneNote CGI presents:
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++
Tutorial 10 Programming with JavaScript
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Welcome to Biology Class of 2014!! Your teacher is: Mrs. Linda Pellegrini This presentation will get you and your laptop ready for this coming year!
CMPS 1371 Introduction to Computing for Engineers MatLab.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
ISU Basic SAS commands Laboratory No. 1 Computer Techniques for Biological Research Animal Science 500 Ken Stalder, Professor Department of Animal Science.
LEARNING HTML PowerPoint #1 Cyrus Saadat, Webmaster.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.
EGR 115 Introduction to Computing for Engineers Introduction to MATLAB Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Tutorial for Arrays and Lists. Description This presentation will cover the basics of using Arrays and Lists in an Alice world It uses a set of chickens.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Matlab for Engineers Matlab Environment Chapter 2.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
Introduction to Computer Programming - Project 1 Intro to Digital Technology.
Introduction to TouchDevelop Lesson 3 – Comments & Lists Created by S. Johnson
IST 210: PHP Basics IST 210: Organization of Data IST2101.
Getting Started With Python Brendan Routledge
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Using Macros Lesson.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
Development Environment
Matlab Training Session 4: Control, Flow and Functions
Matlab tutorial course
Lecture 1: Introduction
Digital Image Processing
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Stata Basic Course Lab 2.
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Running a Java Program using Blue Jay.
Presentation transcript:

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

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

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

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…

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

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

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

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

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!).

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

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!

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

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

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!

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!

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

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

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

Command window

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

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

Workspace Shows you what variables are currently stored in memory

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);

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

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

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

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

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

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

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

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

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

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

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