Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,

Slides:



Advertisements
Similar presentations
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advertisements

Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
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.
Scheduling Discoverer Reports Scheduling Standard Reports Printing & Re-printing Standard Reports Focus on Reports Session 2 To print: Right click Choose.
The Web Warrior Guide to Web Design Technologies
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Programming in MATLAB Week 14 – 4/28/09 Kate Musgrave
Guide To UNIX Using Linux Third Edition
Welcome to the ‘Get up, Get Moving’ Quiz! In this Quiz, you are going to be asked different questions about how you should maintain your body healthy.
Program Input and the Software Design Process ROBERT REAVES.
1 Interactive Applications (CLI) Interactive Applications Command Line Interfaces Project 1: Calculating BMI Example: Factoring the Solution Reading for.
Etomo introduction A) Start “cygwin-bash” on desktop The command prompt window opens up B) Enter “etomo” and hit enter This quick guide will show how to.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Programmer Defined Functions Matthew Verleger. Windows It’s estimated that Window’s XP contains 45 million lines of code (and it’s over 10 years old).
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Functions 1 parameter, 2 return-values "Conversion of time format" One problem. 5 steps to solve it. 1.
1 -Defined Functions 1. Goals of this Chapter 2. General Concept 3. Advantages 4. How it works Programmer.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.
1 Functions Advantages Name, parameters, return info Flow of the data when running functions Menus Functions Applying Functions to Real Projects.
Lecture 4 MATLAB Windows Arithmetic Operators Maintenance Functions
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
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.
Mail merge I: Use mail merge for mass mailings Perform a complete mail merge Now you’ll walk through the process of performing a mail merge by using the.
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. Reminder of Symbols 2. Dialog Boxes 3. listdlg() 4. msgbox() 5. questdlg() 6. menu() Dialog Boxes Applications of Cell-Arrays 1.
GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.
Introduction to File I/O High-Level Functions 1.Data files 2."High level" File I/O 3.dlmread() 4.xlsread() 1.
Lab17 DUE NOW Deliverable 2 Quizzes continue next Lab. on Functions. 1.
CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++
1.Reminder of Symbols 2.Dialog Boxes 3.Output-centric dialogs 4.Input-centric dialogs Dialog Boxes Applications of Cell Arrays 1.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Software Development Process.  You should already know that any computer system is made up of hardware and software.  The term hardware is fairly easy.
File I/O High-Level Functions 1. Definition 2. Is a High-Level function appropriate? 3. xlsread() 4. dlmread() 1.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
Engr 0012 (04-1) LecNotes script/function comparison scriptsfunctions Show program logic answer “what” questions Show program details answer “how”
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.
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
Asking the USER for values to use in a software 1 Input.
11/25/2015Slide 1 Scripts are short programs that repeat sequences of SPSS commands. SPSS includes a computer language called Sax Basic for the creation.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
This document gives one example of how one might be able to “fix” a meteorological file, if one finds that there may be problems with the file. There are.
Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.
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.
1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1.
Review while loops Control variables Example Infinite loop
Programmer-Defined Functions Advantages vs. Disadvantage 1.Definition and general Idea 2.Many Advantages 3.1 Disadvantage 1.
Asking the USER for values to use in a software 1 Input.
Intermediate 2 Computing Unit 2 - Software Development.
-Defined Functions 1. Goals of this Chapter 2. General Concept 3. Advantages 4. Vocabulary 5. Examples 6. General Template – Applications 1. Definitions,
Cell Arrays Lecture 2/8/ Data Types (Review) 2. General Concept 3. Using Cell-Arrays 1. Syntax/Symbols 2. Dialog Boxes 1.
Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
1 The Software Development Process ► Systems analysis ► Systems design ► Implementation ► Testing ► Documentation ► Evaluation ► Maintenance.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Macros in Excel Using VBA Time Required – 5 hours.
Perform a complete mail merge Lesson 14 By the end of this lesson you will be able to complete the following: Use the Mail Merge Wizard to perform a basic.
Python Lesson 12 Mr. Kalmes.
Key Ideas from day 1 slides
Dialog Boxes Applications of Cell-Arrays
Using the Excel Creation Template to Create a Variable Parameter Problem (Macro Enabled “Alpha 1.4.2”) Getting started – Example 1 Note – You should be.
Python Lesson 12 Mr. Kalmes.
Scripts & Functions Scripts and functions are contained in .m-files
User Defined Functions
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Using Script Files and Managing Data
Getting started – Example 1
Presentation transcript:

Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info, Documentation, and Code Body, saving it. 3. Testing in the command window 4. Testing with a main file 5. A new client comes along 1. Ignoring return values 1

2 1. The client's wish: Create a function which receives 3 arguments (a height, a mass and the unit system used) and calculates the BMI value (body-mass-index: a measure of body fat) and the BMI category. The client also gives these as indications: BMI Category depends on BMI value: Underweight <=18.5 Normal weight = Overweight = Obesity = BMI of 30 or greater

Step2. I/O of the function 3 parameters, 2 return-values 3 bmiCalculator() Unit system Bmi value weight height Bmi category

2. Determine the return-info Create a function which receives 3 arguments (a height, a mass, and the unit system used) and calculates the BMI value (body-mass-index: a measure of body fat) and the BMI category. 4 Data calculated becomes return-info. When more than one variable comes back enclose the return list inside [ ].

2. Choose a function name Create a function which receives 3 arguments (a height, a mass, and the unit system used) and calculates the BMI value (body-mass-index: a measure of body fat) and the BMI category. 5 Name it so it represents what it does

2. Create the parameter list Create a function which receives 3 arguments (a height, a mass, and the unit system used) and calculates the BMI value (body-mass-index: a measure of body fat) and the BMI category. 6 Inputs received become parameters. Separated by COMMAS all in ( ).

2. Save the function Save the function in the directory of your choice, Hit F5 to run if you wish, but knowing that it'll crash right after changing your directory. 7 But keep the filename MATLAB gives you by default. DO NOT CHANGE IT, UNDER ANY CIRCUMSTANCES.

2. Create the Documentation Create a function which receives 3 arguments (a height a mass and the unit system used) and calculates the BMI value (body-mass-index: a measure of body fat) and the BMI category. 88 Must use single % only without skipping one!.

2. Create the function-body 99 BMI Category depends on BMI value: Underweight <=18.5 Normal weight = Overweight = Obesity = BMI of 30 or greater

3. "Flow" of data Values will come from the arguments in the function-call.

3. "Flow" of data Using only the given parameters, this code executes, thus solving for the return values. One day, if needed, you may define new variables to compute intermediate steps. Remember that all these variables are deleted when the function is done with its job.

3. "Flow" of data MATLAB returns the values.

Make sure you have all this typed correctly. 13

3. Testing - Experiment Test the help! 14 Error from hitting F5 Then, testing the documentation

3. Testing - Experiment This is the ‘experimental’ stage! 15 Hardcoded arguments just to test! The function returns two VALUES. >>>>>> Remember to create two VARIABLES capable of holding those two values.

16 >>[x,y]=bmiCalculator(1.65,55,'metric') 1. PASS 2. USE and CALCULATE 3. STORE RESULTS IN RETURN VARIABLES 4. RETURN 5. COLLECT " Flow " of data

17 4. Testing - Main file There will now be 2 files in the current directory: The algorithm is: % prompt unit system % prompt height and weight % calculate bmi and status % display both

18 4. Code1 – old fashion 1. Ask for inputs. 2. Pass arguments to the function. 4. Show results. 3. Collect the return values.

4. Hit F5 to run This is a regular script file, so hitting F5 is valid. 19

4. Code 2 – dialog boxes questdlg() inputdlg() msgbox() 20

21 Don't hesitate to practice this code. It's fun! This is the function- call

Almost Wrapping Up Meet with the client, give the software, go on vacations… But another client just walks in, having heard of the bmiCalculator… His request is slightly different: "I just care about the category, not the actual value of the bmi. Can you help me?" 22

5. Ignoring Return Values YES! DO NOT DELETE ANYTHING FROM THE FUNCTION. There is a way to ignore return-values. 23

5. Ignoring Return Values Not this way… 24 MATLAB ignored the 2 nd return value, and stored the first one in y.

25 5. Ignoring Return Values Suppose a stack of books What if you want the 3 rd book (the yellow one) from the top? You must collect the 1 st and 2 nd (red and green) before collecting the 3 rd (yellow). It is exactly the same with the return-values. They come out in order. To access the 3 rd return value the code must collect the 1 st and 2 nd.

5. Ignoring Return Values >>>>>>> Every variable UP TO the one wanted must be collected in a variable. (not the ones after) Trick: name the variable trash, dummy, ignore, a word that obviously reflects that it is not-to-be used later. You may even delete that variable specifically using clear 26 Try it! Look at the Workspace.

5. Ignoring Return Values Use the 'tilde' operator 27 The ~ indicates "there is no need to store it". COMMA IS MANDATORY. SAME function, DIFFERENT call. Yay! MATLAB version 7.9 (R2009b) and up ONLY

28 Previous version of MATLAB Previous versions of MATLAB do not allow the use of the ~ symbol. Use a dummy variable name to store the data. The name reminds YOU (programmer) that this variable is of no use later on, so choose wisely! It is a MUST to collect all the return data to the left of the one wanted.

29 Option 2 cont. Press F5 on the previous script file to see results in the Command Window.

Wrapping Up Go type this example step-by-step and make it work. At most, this should take 15 minutes. In order: 1. Create a function file. 2. Save it and change to the directory where it is. 3. Test and experiment in the command window 4. Create the main file afterwards Only practice makes perfect, and the more functions you create, the easier codes will get: INCLUDING the final project! 30