Functions Scripts and Workspace scripts are just a collection of commands, running a script is same as running the statements in command window. Your.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
Lecture 5.
COMP 116: Introduction to Scientific Programming Lecture 37: Final Review.
Introduction to Matlab
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
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.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Why care about debugging? How many of you have written a program that worked perfectly the first time? No one (including me!) writes a program that works.
Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
User-defined Functions. CS 1112 Scripts Command window: x = 2; my_script Hello! y = x + 2 y = 7 my_script.m: disp( 'Hello!' ); x = 5;
Example 2.
Loops II. For Loop For = execute statements for each value of index end for x = [ ] disp(num2str(x^2)); end
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
User-defined Functions Selim Aksoy Bilkent University Department of Computer Engineering
C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB
MATLAB Programming fprintf Cell arrays Structures Flow of control Vectorization Functions 1.
Wed/Fri Week 2 Functions! What are they? What do they look like in JavaScript? What are they good for? How do I use them? Some examples… Mini-Lab 1!!!
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
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.
COMP 116: Introduction to Scientific Programming Lecture 7: Matrices and Linear Systems.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 6.
A First Book of ANSI C Fourth Edition
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 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS Introduction to Computing for the Physical Sciences.
Matlab Programming, part 1 M-files It is generally more convenient to program in Matlab using m-files, ascii text files containing a set of Matlab commands.
E0001 Computers in Engineering Procedures: subprograms and functions.
 Expression Tree and Objects 1. Elements of Python  Literals, Strings, Tuples, Lists, …  The order of file reading  The order of execution 2.
1 Lab 2 of COMP 319 Lab tutor : Shenghua ZHONG Lab 2: Sep. 28, 2011 Data and File in Matlab.
Programming Logic and Design Using Methods. 2 Objectives Review how to use a simple method with local variables and constants Create a method that requires.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
COMP 116: Introduction to Scientific Programming Lecture 11: Functions.
Engr 0012 (04-1) LecNotes script/function comparison scriptsfunctions Show program logic answer “what” questions Show program details answer “how”
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
David Stotts Computer Science Department UNC Chapel Hill.
Introduction MATLAB stands for MATrix LABoratory.  Basics  Matrix Manipulations  MATLAB Programming  Graphics  Image types  Image Processing  Useful.
Python Functions.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
A string is an array of characters Strings have many uses in MATLAB Display text output Specify formatting for plots Input arguments for some functions.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
User defined functions
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.
Array Operations ENGR 1181 MATLAB 4.
Introduction to Matlab
Computer Science I Arrays. Parallel structures. Classwork/Homework: Build your own bouncing things.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Recap Functions with No input OR No output Determining The Number of Input and Output Arguments Local Variables Global Variables Creating ToolBox of Functions.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
JavaScript for C# developers Dhananjay Microsoft MVP
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
The Hong Kong Polytechnic University Industrial Centre 1 MatLAB Lesson 7 : M-file Programming I Edward Cheung Room W311g 2008.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
Expressions and Data Types Professor Robin Burke.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
Scripts & Functions Scripts and functions are contained in .m-files
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
إستراتيجيات ونماذج التقويم
Writing functions in MATLAB
Predefined Functions Revisited
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Presentation transcript:

Functions

Scripts and Workspace scripts are just a collection of commands, running a script is same as running the statements in command window. Your scripts and command window shares the same set of variables, also called the workspace.

Passing a variable to a script Command Window: >>x=2; >>script 2 >>x x = 5 script.m : disp(num2str(x)) x = 5;

Global Variables Variables in scripts and command window are shared, in other words, they are global. Global variables have been found to be problematic, since your program could erase a value needed by some other script, or the values you depend on might be changed by some other program. These side-effects are difficult to find In large systems, name clashes would be very common.

Passing Data to a Script Let’s say we have a script that sums the values in a vector : sum_vect.m : sum = 0; for x=1:length(v) sum = sum + v(x) end

Passing Data to a Script We have another program where we are given three vectors: a,b, & c. We want to take the average of all elements in them. How can we make use of sum script? v = a; sum_vect, suma = sum; v = b; sum_vect, sumb = sum; v = c; sum_vect, sumc = sum; average = (suma+sumb+sumc)/(length(a) + length(b)+length(c))

A better way? Take the rem function Do we call it like: >>numerator=7,denominator = 3; rem; disp(num2str(result)) We do: >>disp(num2str(rem(7,3))); We just send our data as parameters, and the expression will be evaluated to return value of our function

Horizontal Line Example Rather than getting the input from user every time, we can write our own function horizontalLine: function horizontalLine(a, b, y, g) for x=a:b-1 putPixel(x, y, g); end >>>> horizontalLine(10, 300, 20, g)

Drawing a Rectangle function drawRectangle(x1, y1, x2, y2, g) x = y1; while (x <= y2) horizontalLine(x1, x2, x, g) x = x + 1; end what about x? horizontalLine also have a variable x !

Functions have private workspaces >> whos Name Size Bytes Class fr 1x1 javax.swing.JFrame g 1x1 sun.awt.windows.WGraphics mines 400x logical array Grand total is elements using bytes >> horizontalLine(10, 200, 20, g) >> x ??? Undefined function or variable 'x'.

Functions as Black Boxes Functions are machines that get some input and produce some output. We cannot see, or shouldn’t care to see the inner workings of a function, the local variables used by the function and so on. functions cannot see or use variables that exist in the context of the caller. when you pass input arguments to functions, you are not passing variables, just simple values.

Function calls function x a = 10; x1(a) function x1(a1) a1 =20; x2(a1) function x2(a1) a1 =30; x3(a1 + 10) function x3(a3) a1 =20; a=10 a1=20 a1=30 a3=40 a1=20 the only information that goes from a function to another is the values of parameters. The callee cannot see or know which variables the caller has

A closer look at putPixel function suite initWindow : fr = javax.swing.JFrame('Drawing Pad'); fr.setSize(400, 400); mines = rand(400, 400) > 0.99; fr.show; g = fr.getContentPane.getGraphics; it creates global variables mines and g

PutPixel function function putPixel(x, y, g) g.drawLine(x,y, x,y) Since putPixel cannot see the global variable g, we have to pass it ourselves Note that we could also write: function putPixel(x, y, graphicsObject) You can give any variable name you want

Functions that Return values function minValue = minVector(v) minValue = v(1); for i = 2:length(v) if (v(i) < minValue) minValue = v(i); end >> v = [ ] v = >> minVector(v) ans = 1

Function that Returns multiple values function [minValue, minIndex] = minVector(v) minValue = v(1); minIndex = 1; for i = 2:length(v) if (v(i) < minValue) minValue = v(i); minIndex = i; end >> minVector(v) ans = 1 >> [a b] = minVector(v) a = 1 b = 4

Complete Form function [minValue, minIndex] = minVector(v) %MINVECTOR function that returns minimum of a vector % it returns two values, one the minimum % value in the vector, the other, the index of the minimum value minValue = v(1); minIndex = 1; for i = 2:length(v) if (v(i) < minValue) minValue = v(i); minIndex = i; end

help and lookfor >> lookfor minVector MINVECTOR function that returns minimum of a vector >> >> help minVector MINVECTOR function that returns minimum of a vector it returns two values, one the minimum value in the vector, the other, the index of the minimum value