Matlab tutorial course

Slides:



Advertisements
Similar presentations
Introduction to Applications & Basic Features. What is MATLAB®? MATLAB® /Simulink® is a powerful software tool for: Performing mathematical computations.
Advertisements

Matlab tutorial course Lesson 1: The Basics: variables, scripts and functions Dr Michael Berks
Tutorial 12: Enhancing Excel with Visual Basic for Applications
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.
CIS101 Introduction to Computing Week 09 Spring 2004.
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.
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
A First Program Using C#
Introduction to Python
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.
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:
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.
Guide to Programming with Python Chapter One Getting Started: The Game Over Program.
CMPS 1371 Introduction to Computing for Engineers MatLab.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
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 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
Introduction to TouchDevelop Lesson 3 – Comments & Lists Created by S. Johnson
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.
PROBLEM SOLVING WARM-UP Fill in the spaces using any operation to solve the following (!, (), -/+,÷,×): = 6.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
Basic Programming I Working with QBasic
Physics 114: Lecture 1 Overview of Class Intro to MATLAB
Development Environment
Topic: Python’s building blocks -> Variables, Values, and Types
Release Numbers MATLAB is updated regularly
A Playful Introduction to Programming by Jason R. Briggs
Computer Fundamentals 1
D.Y.O. Web The new and easy way to create and maintain your own professional dynamic website.
Python Let’s get started!.
Matlab Training Session 4: Control, Flow and Functions
Single Sample Registration
Introduction to Programming the WWW I
Basic operations in Matlab
Adding Assignments and Learning Units to Your TSS Course
INTRODUCTION TO BASIC MATLAB
CS 240 – Lecture 11 Pseudocode.
MATLAB DENC 2533 ECADD LAB 9.
User Defined Functions
What is Google Classroom?
Macrosystems EDDIE: Getting Started + Troubleshooting Tips
Lab 1 Introductions to R Sean Potter.
Lecture 1: Introduction
Introduction to TouchDevelop
Matlab tutorial course
Learning Outcomes –Lesson 4
Introduction to TouchDevelop
Digital Image Processing
Macrosystems EDDIE: Getting Started + Troubleshooting Tips
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Stata Basic Course Lab 2.
Part 1. Preparing for the exercises
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.
Macrosystems EDDIE: Getting Started + Troubleshooting Tips
Macrosystems EDDIE: Getting Started + Troubleshooting Tips
Running & Testing :: IDEs
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Matlab tutorial course + Python Matlab tutorial course Dr Michael Berks michael.berks@manchester.ac.uk Lesson 1: The Basics: variables, scripts and functions

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

What is a computer program? What is Matlab? What is a computer program? 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 (or phone, tablet etc)… 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…

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

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

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

In medical imaging… What is a computer program? DATA PROGRAM RESULT Tell doctor/drugs company if the drug is working RESULT Measure change in size of tumour… PROGRAM 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. A chunk of commands in a specific programming language… What is ‘code’? 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? Now the big question…. Why do I need to learn how to write my own computer code? Think of it like learning to cook…..

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

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

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

Using other people’s code 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). Excel Photoshop Using other people’s code Writing your own code + + + = A really powerful tool for imaging research!

General purpose programming language But what about…. Python? General purpose programming language Packages provide support for scientific computing Open-source Image source: http://nbviewer.jupyter.org/github/jrjohansson/qutip-lectures/blob/master/Lecture-1-Jaynes-Cumming-model.ipynb

 

How does it differ from Matlab? It’s free No need for an expensive license Widely used, both in academia and industry Often installed as part of OS No single development environment This can make it a little trickier get started But, also more flexible

Popular Packages Multidimensional Arrays/Matrices Machine Learning Image Processing Plotting and Visualisation Signal Processing and Optimisation Deep Learning and many more…

Development Tools Interactive Computing Environment Text Editor with Python Support Full Integrated Development Environment (IDE) Notes: We can flip over to each of these to show what they are Scientific IDE (Matlab like) : Installed as part of Anaconda

Course aims To show you how to use Matlab and/or python 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 projects To make some pretty pictures!

I’m a bit confused… Should I use Matlab… ..or Python?

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: http://personalpages.manchester.ac.uk/staff/michael.berks 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

How to install python Use a scientific Python distribution: Anaconda Includes everything you need to get started. https://www.anaconda.com/download

Lesson Overview Using the Matlab interface (or try Spyder) 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

Command window A fancy scientific calculator Try using ↑ and ↓ 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 Can also self-assign: Keeps data in memory to use again a = 4 b = 3 + 6 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 Checks if it is a variable in the current memory space Looks for a script or function in the Matlab path 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