Functions M-file with the function keyword. First line: –[myout1, myout2,..] = myfunction(a, b,…) Function called by name of m-file, not function name,

Slides:



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

Introduction To MATLAB Prof. Muhammad Saeed Mathematical Modeling and Simulation.
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Getting Input in Python Assumes assignment statement, typecasts.
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
Matlab Tutorial Course
User-defined Functions Selim Aksoy Bilkent University Department of Computer Engineering
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Chapter 7: User-Defined Functions II
Homework #2: Functions and Arrays
PHP –Writing Reusable Code 10 March 2006 Adina Crainiceanu IT420: Database Management and Organization.
An Introduction to Programming with C++ Fifth Edition
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
CMPUT 101 Lab #6 October 29, :00 – 17:00. Array in C/C++ Array is a structure type variable. One dimension of array int: int num[3]; There are.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Computer Science 1620 Programming & Problem Solving.
User-defined Functions Selim Aksoy Bilkent University Department of Computer Engineering
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
MATLAB and Simulinklecture 31 To days Outline  Functions  Strings  Sparse Arrays  Cell Arrays  Structures  Exercises on this days topics.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
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.
SOLVING SUDOKU WITH MATLAB VERIFICATION FUNCTION correctness verification of the puzzle: checks if the current element appears twice in the same line,
Fall 2006AE6382 Design Computing1 OOP: Creating a Class More OOP concepts An example that creates a ASSET class and shows how it might be used Extend the.
Introduction to Matlab & Data Analysis
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
How to think through your program [ principles of good program design ] Rachel Denison MATLAB for Cognitive Neuroscience ICN, 13 December 2007.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
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.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox.
Homework #3: Classes and Constructors
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
MA/CS 3751 Fall 2002 Lecture 24. MA/CS 3752 ginput ginput is a Matlab function which takes one argument input: number of points to select in the image.
Matlab Training Session 10: Loading Binary Data Course Website: Training Sessions.htm.
SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 6”
Digital Image Processing Lecture10: MATLAB Example – Utility M-functions for Intensity Transformations.
CMPS 1371 Introduction to Computing for Engineers MatLab.
Flow Control and Functions ● Script files ● If's and For's ● Basics of writing functions ● Checking input arguments ● Variable input arguments ● Output.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
COMP 116: Introduction to Scientific Programming Lecture 11: Functions.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
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.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 8: Simple Data Types, Namespaces, and the string Type.
Fall 2006AE6382 Design Computing1 OOP Programming in Matlab Review our ASSET class and study how it might be used as a parent for other classes Extend.
Introduction to CMex E177 April 25, Copyright 2005, Andy Packard. This work is licensed under the Creative.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
User Defined Functions Spring EE 201. Class Learning Objectives  Achieve Comprehension LOL of User Defined Functions. Spring
Introduction to programming in java Lecture 21 Arrays – Part 1.
Creating a script create new blank document. Editor options Docking and undocking tabs.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Java Programming Language Lecture27- An Introduction.
Chapter 9: Value-Returning Functions
EGR 115 Introduction to Computing for Engineers
Scripts & Functions Scripts and functions are contained in .m-files
Functions In Matlab.
ME 123 Computer Applications I Lecture 5: Input and Output 3/17/03
Presentation transcript:

Functions M-file with the function keyword. First line: –[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 Function variables have local scope. Use the ;, otherwise it will appear that they are accessible from outside the function. Try it.

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.

Accessing elements of arrays If a = [ ] a(3) = 45 a(3:5) = [45 6 7] [a(3:5) a] = [ ]

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.

Introduction to Matlab GUI Helped by guide.