Basics of MATLAB 2- Programming in MATLAB By DR. Wafaa Shabana

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Programming with MATLAB
MATLAB for Image Processing April 10 th, Outline Introduction to MATLAB –Basics & Examples Image Processing with MATLAB –Basics & Examples.
Introduction to MATLAB
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB.
Dr. Jie Zou PHY Introduction to MATLAB Programming with MATLAB 1 1 Applied Numerical Methods with MATLAB for Engineers and Scientists, 2nd ed., Steven.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Programming in MATLAB Week 14 – 4/28/09 Kate Musgrave
Al-Amer An Introduction to MATLAB Dr. Samir Al-Amer Term 062.
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
Digital Image Processing Lecture3: Introduction to MATLAB.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
CS112 Scientific Computation Department of Computer Science Wellesley College Numb3rs Number and image types.
Matlab The language of Technical computing Mr. D. Suresh Assistant Professor, Dept. of CSE, PSNA CET, Dindigul.
Introduction to MATLAB. CSPP58001 MATLAB MATLAB is is a matrix-based language well suited for carrying out numerical analysis. It has many, many high-
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Digital Image Processing Lecture4: Fundamentals. Digital Image Representation An image can be defined as a two- dimensional function, f(x,y), where x.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB Lecture 18.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
CSE123 Lecture 3 Files and File ManagementScripts.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
INTRODUCTION TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
Matlab Programming for Engineers
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
EEE 161 Applied Electromagnetics
Matlab Training Session 4: Control, Flow and Functions
The Selection Structure
Basic operations in Matlab
Scripts & Functions Scripts and functions are contained in .m-files
Outline Matlab tutorial How to start and exit Matlab Matlab basics.
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
Matlab review Matlab is a numerical analysis system
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
CS100J 26 April. Matlab Use help button!!! Variables, values, types
WEB PROGRAMMING JavaScript.
Use of Mathematics using Technology (Maltlab)
MATLAB Tutorial Dr. David W. Graham.
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
CSE107 Matlab Introduction
Digital Image Processing
INTRODUCTION TO MATLAB
CSE 307 Basics of Image Processing
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Topics Designing a Program Input, Processing, and Output
Experiment No. (1) - an introduction to MATLAB
Topics Designing a Program Input, Processing, and Output
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Basics of MATLAB 2- Programming in MATLAB By DR. Wafaa Shabana

Table of Contents: 1. Introduction 2. MATLAB m-files. 3. Flow control in MATLAB. 4. Image Processing using MATLAB 5. Questions? 6. References

1. Introduction: Recall that: Everything in MATLAB is a matrix ! The MATLAB environment is command oriented somewhat like UNIX. If a statement is terminated with a semicolon ( ; ), no results will be displayed. Otherwise results will appear before the next prompt. ansDefault variable name for results % start comment which ends at end of line

The colon operator ( : ) to specify range MATLAB supports six relational operators. Less Than< Less Than or Equal<= Greater Than> Greater Than or Equal>= Equal To== Not Equal To~=

MATLAB supports three logical operators. not~% highest precedence and&% equal precedence with or or |% equal precedence with and

MATLAB is case-sensitive Programming language. Variable names can contain up to 63 characters (as of MATLAB 6.5 and newer) Variable names must start with a letter followed by letters, digits, and underscores Examples: my_name, day2 % valid variable names Invalid variable name: my-name %minus not allowed 2day %must start with letter %x % not allowed %not allowed character

Some of useful MATLAB commands whoList known variables whos List known variables plus their size help>> help sqrt Help on using sqrt lookfor>> lookfor sqrt Search for keyword sqrt in m-files what >> what a: List MATLAB files in a: clear Clear all variables from work space clear x y Clear variables x and y from work space clc Clear the command window (clear the screen) save saves the workspace load loads a saved workspace or data file

2. MATLAB m-files: There is no doubt that you can do a lot of work in the command window. However, when the amount of instructions needed to complete a task increases or you need to re-execute a block of instructions several times, the command window is a poor choice. The best choice is to write a MATLAB program.

MATLAB programs are stored as text files. The files can be loaded into the MATLAB command environment where they are then interpreted and executed. MATLAB supports two types of programs: 1- script files and 2- function files.

Script files consist of sequences of instructions stored in text files. The files are commonly referred to as m-files because of the.m extension used. An m-file can be created by any text editor or word processor. If a word processor is used, make sure to save the file as a text file. MATLAB has its own built-in text editor. To launch it, type the command edit in the command window

Example (1): %this is our first m.file %saved as first.m %plot a graph of a cos function clear t = 0: 0.001:10; y = cos (2*pi*t); plot(t,y) title(‘My first graph’) xlabel(‘time, sec’) ylabel(‘values of y’)

Remark: 1- Before trying to run your program, make sure that its file is on the MATLAB path. 2- Use the which function to see if your program is on the path: which first 3-You have the choice of operating the debugger from the Editor window that displays your program, from the MATLAB command line, or both.

Function files: The main difference between a script and a function is that a function accepts input from and returns output to its caller, whereas scripts do not. You define MATLAB functions in a file that begins with a line containing the function key word. Question: Can we define a function within a script file or at the command window?

* Functions always begin with a function definition line for example function y = average(x) The function ends either with the first matching end statement, the occurrence of another function definition line, or the end of the file, whichever comes first.

Example: function y = area(x1,x2) %this function computes the area of a rectangle %input: length x1 and width x2 %output: area y x1 =input('Enter the width:'); x2 =input('Enter the length:'); y = x1 * x2; disp(‘The area is %d’, y)

input Request user input Syntax evalResponse = input(prompt) strResponse = input(prompt, 's') disp Display text or array Syntax disp(X)

If the function is supposed to return more than one value, the syntax is: function [output_1,output_2,…]= function_name(input_1, input_2,…) Question: What is the difference between MATLAB functions and C- functions? Note that: The use of the editor is pretty straight-forward. The advantage of using MATLAB’s editor is that color coding and formatting is built in.

3. Control flow in MATLAB: Decision in MATLAB: 1- if statement: if (logical statement) block of code to execute if logical statement is true end 2- if …………else statement: if (logical statement) block of code to execute if logical statement is true else block of code to execute if logical statement is false end

3- Nested if else statement: if (logical statement 1) block of code to execute if logical statement 1 is true elseif (logical statement 2) block of code to execute if logical statement 2 is true elseif (logical statement n) block of code to execute if logical statement n is true else block of code to execute if all logical statements are false end

4- switch statement: switch evaluative expression case value_1 block of code to execute if the evaluative expression is value_1 case value_2 block of code to execute if the evaluative expression is value_2 case value_n block of code to execute if the evaluative expression is value_n otherwise block of code to execute if the evaluative expression is not any of the values end

2- Iteration in MATLAB: 1- for statement: for i = 1:10 % execute these commands end 2- while statement: while x <= 10 % execute these commands end

break statement: Terminate execution of for or while loop Syntax break return statement: Return to invoking function Syntax return continue statement: Pass control to next iteration of for or while loop Syntax continu e

4. Image Processing in MATLAB: Image processing toolbox is one of the most useful toolboxes in MATLAB. This toolbox supports a wide range of image processing operations. There are five types of images in MATLAB: 1- Binary image : {0, 1}{black white} 2- Gray-scale image: [0,255] {shades of gray} 3- True color image: m x n x 3 4- Intensity image: [0, 1] or uint8 5- Indexed images: m x 3 color map

1- Reading an image: I = imread(‘image name’) It reads an image from an image file and store it In a matrix I 2- displaying an image: imshow(‘image_name’) 3- image size size(‘image_name’)

Check the Image in Memory whos Name Size Bytes Class ans 291x uint8 array Grand total is elements using bytes uint8 [0, 255] uint16 [0, 65535] double [0, 1]

Resizing Images B = imresize(A, scale) B = imresize(A, [mrows ncols])

For example: clear close all y= imread('pic2.jpg'); I1 = imresize(y, 0.5); I2 = imresize(y,[ ]); figure imshow(y) figure imshow(I1) figure imshow(I2)

im2bw Convert image to binary image, based on threshold Syntax BW = im2bw(I, level) BW = im2bw(X, map, level) BW = im2bw(RGB, level) rgb2gray Convert RGB image or colormap to grayscale Some useful commands:

image Display image object imwrite Write image to graphics file Syntax imwrite(A,filename,fmt) e.g. imwrite(I, ‘wafaa’, ‘gif’)

5. Questions? (Mini project in MATLAB): 1- Encoding step: Write a m-function that hides some information (e.g. : your name) into a given image. (watermarked image) 2- Decoding Step: Write an m-function that receives the watermarked image and extract the water mark from it.

6. References: You can read the following references: 1- “The Basics of MATLAB” Jeffrey O. Bauer “MATLAB tutorial for beginners” Jyotirmay Gadewadikar Need further help? Contact me

Thank you for your attendance and hope you have not wasted your time.