Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.

Slides:



Advertisements
Similar presentations
Introduction To MATLAB Prof. Muhammad Saeed Mathematical Modeling and Simulation.
Advertisements

Introduction to Matlab
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
R for Macroecology Aarhus University, Spring 2011.
Functions M-file with the function keyword. First line: –[myout1, myout2,..] = myfunction(a, b,…) Function called by name of m-file, not function name,
Introduction to MATLAB The language of Technical Computing.
Introduction to Matlab
User-defined Functions Selim Aksoy Bilkent University Department of Computer Engineering
M AT L AB Programming: scripts & functions. Scripts It is possible to achieve a lot simply by executing one command at a time on the command line (even.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
ES 314 Lecture 2 Sep 1 Summary of lecture 1: course overview intro to matlab sections of Chapters 2 and 3.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
Introduction to MATLAB and image processing. MATLAB and images The help in MATLAB is very good, use it! An image in MATLAB is treated as a matrix Every.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
Program Design and Development
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/22/2003.
1 Introduction to MatLab MatLab stands for Matrix Laboratory. As the name suggests most of the programming operations have as input or output a matrix.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
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.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
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.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Matlab for Engineers Manipulating Matlab Matrices Chapter 4.
CMPS 1371 Introduction to Computing for Engineers MatLab.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
COMP 116: Introduction to Scientific Programming Lecture 11: Functions.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
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.
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences.
Introduction to Matlab
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
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.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
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.
SCRIPTS AND FUNCTIONS DAVID COOPER SUMMER Extensions MATLAB has two main extension types.m for functions and scripts and.mat for variable save files.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Manipulating MATLAB Matrices Chapter 4
Matlab Training Session 4: Control, Flow and Functions
Scripts & Functions Scripts and functions are contained in .m-files
Introduction To Matlab Class 1
Use of Mathematics using Technology (Maltlab)
Functions In Matlab.
Communication and Coding Theory Lab(CS491)
INTRODUCTION TO MATLAB
CSE 307 Basics of Image Processing
Matlab Basics Tutorial
Matlab Basics.
Computer Simulation Lab
Presentation transcript:

Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements Functions Input and output

Declaration of a variable One of the first things you might want do do in a programming language is to declare a variable. In Matlab A=5.8 B=4.5 We can add subtract, multiply and divide

Simple operations We can add subtract, multiply and divide. A+B A-B The answer is returned in the system generated variable “ans”

Simple operations We can add subtract, multiply and divide. A+B A-B The answer is returned in the system generated variable “ans” Alternatively we can assign the answer to our own new variable C=A+B

Matrices and vectors syntax [] Matlab treats all variables as arrays; as seen from the workspace window. So we can assign variable to vectors (1D array) and matrices (2D array) directly as follows D=[ ] (vector) E=[1 2 3; 4 5 6; 5 6 7] (matrix) Note the use of the semicolon to separate rows. (Can also use carriage return) We will use matrices for audio and video.

Step or range syntax : –1:8 generates all the numbers between 1 and 8 (steps of 1) –1:2:9 generates numbers from 1 to 9 in steps of 2. –As an index A(9:12, :) returns the rows in the range 9 to 12, and the range of all the columns (very useful for us).

Control (for, if, while…) A=3 for J=1:8 A=A+1 end B=7 if B==7 C=10 elseif B==5 C=11 else C=100 end

Control (for, if, while…) C=1 while C~=4 C=C+1 end Use Matlab help (command line) to find switch, break and continue. help switch

m-files Matlab scripts can be written and saved as an “m-file”. They have a.m extension. The are simply a series of program steps saved for later execution. To run the script type the name of the m-file. Make the above “for” loop into a script m-file.

Functions M-file with the function keyword. First line: function [myout1, myout2,..] = myfunction(a, b,…) Function called by name of m-file, not function name, but keep them the same. Remember to save modifications.

Functions Unlike scripts, function variables have local scope. Clear the work space variables (“clear”) (and observe workspace window) So if we repeat the “for” loop example as a function the Matlab workspace will not know any thing about the variables B and C. But we can retrieve the answer by assignment –D=myfor()

Functions Global keyword allows global access to the variables. Declare variable global: – global x (after top line in function) Check the scope of x in the workspace and in other functions. Need to declare x as global everywhere we need to access it as global.

Input/Output a = input( ‘give me some input’) a = input( ‘give me some input’, ‘s’) a = input( ‘give me some input \n’) disp(‘myname’) –name = ‘myname’ –disp(name)

Exercise Write a function to input a name and output it after asking for a response. The function should only display the prompts and typed input. You will have to use some control structure (use ‘y’ and ‘n’).

Cell arrays Use with care. Each element can contain different types. a = { 56, ‘fifty-seven’, 8} Compare with: – a = [56, ‘fifty-seven’, 8]

Class Class(a) returned variables class or type. Generally variables are of type double, but can be cast. a = 5 class(a) is double precision b = uint8(a) class(b) is 8 bit unsigned integer Some operations +- etc not applicable to certain types.

Variable numbers of inputs to functions Suppose we don’t know how many inputs will be given to a function. Example: –Optional arguments. –An list of undetermined length. Use varargin after any compulsory arguments.

Variable numbers of inputs to functions a = myfunction(a, b, varargin) After a and b have been input (compulsory in this case) any extra inputs are put into the cell array varargin and can be used in your function. See “help varargin”

Variable numbers of outputs from functions. Suppose we don’t know how many outputs will be taken from a function. Example: –General functions –Extracting the first answers from a list. Use varargout after any necessary output varaibles.

Variable numbers of outputs from functions function [s,varargout] = myfunction(x) You can fill the cell array varargout with as many different variables as you like. When you call the function you then don’t have to take all of the variables. [a, b, c] = myfunction(x) takes s and the first two variables in varargout.

Exercises Write a Matlab function to take three numbers in and return 2. Test it with the wrong numbers of inputs and outputs. Write a Matlab function to take a least 2 inputs and return 2. (use varargin/out) Test it with different numbers of inputs and outputs.

Matlab for images Note the other use of the semicolon. A=imread(`picture.bmp’, ‘bmp’); imwrite(A, ‘mypiccy.bmp’, ‘bmp’); image(A) or imagesc(A)

Matlab for sound A=wavread(‘mysound.wav’) there are other forms. wavwrite(A, ‘filename’) wavplay(A, Fs)