Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.

Slides:



Advertisements
Similar presentations
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect FXP. 1Winter Quarter Course Wrap Up and Final Review Topics Lecture.
Advertisements

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Introduction to Matlab
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.
Engineering EG167C - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review.
The Web Warrior Guide to Web Design Technologies
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 20P. 1Winter Quarter Propulsion Lab with MATLAB Lecture 20.
Lecture 2 Introduction to C Programming
Introduction to C Programming
MATLAB’s extensive, device-independent plotting capabilities are one of its most powerful features. They make it very easy to plot any data at any time.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect FXP. 1Winter Quarter Course Wrap Up and.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
Introduction to MATLAB
Lecture 7 Sept 17 Goals: Complete Chapter 4 Chapters 5 and 6.
Lecture 7 Sept 29 Goals: Chapters 5 and 6. Scripts Sequence of instructions that we may want to run can be stored in a file (known as script). by typing.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB.
Computer Programming 1 Functions. Computer Programming 2 Objectives Take a first look at building functions Study how a function is called Investigate.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 17P. 1Winter Quarter Personal Libraries.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M2P. 1Winter Quarter Midterm 2 Review Topics.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect FXP. 1Winter Quarter Course Wrap Up and.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.
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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 15P. 1Winter Quarter Arrays and Pointers.
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.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
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.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
MAE 1202: AEROSPACE PRACTICUM An Introduction to MATLAB: Part 2 Mechanical and Aerospace Engineering Department Florida Institute of Technology Developed.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
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 &
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB Lecture 18.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
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.
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
Files: By the end of this class you should be able to: Prepare for EXAM 1. create an ASCII file describe the nature of an ASCII text Use and describe string.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
Introduction to Matlab
Introduction to MATLAB Section2, statistics course Third year biomedical dept. Dina El Kholy, Ahmed Dalal.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 20P. 1Winter Quarter Propulsion Lab with.
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 on MATLAB Ecological Modeling Course Sep 11th, 2006.
Structured Computer Programming EE 201 Introduction to MATLAB 7 for Engineers بسم الله الرحمن الرحيم King Abdulaziz University College of Engineering Dept.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
User-Written Functions
Computer Application in Engineering Design
MATLAB: Script and Function Files
Scripts & Functions Scripts and functions are contained in .m-files
Outline Matlab tutorial How to start and exit Matlab Matlab basics.
MATLAB: Structures and File I/O
MATLAB: Script and Function Files
Matlab review Matlab is a numerical analysis system
Plotting Multiple Graphs In The Same Plot
Simulation And Modelling
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
MATLAB: Script and Function Files
Using Script Files and Managing Data
Presentation transcript:

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and Function Files Lecture 19

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 2Winter Quarter MATLAB Script Files A MATLAB script file (Called an M-file) is a text (plain ASCII) file that contains one or more MATLAB commands and, optionally, comments. The file is saved with the extension ".m". When the filename (without the extension) is issued as a command in MATLAB, the file is opened, read, and the commands are executed as if input from the keyboard. The result is similar to running a program in C. The following slide is the contents of an M-file. Note that there are no prompts (>>).

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 3Winter Quarter MATLAB Script Files % This is a MATLAB script file. % It has been saved as “g13.m". load g13.dat; %Load data file voltage = g13( :, 4); %Extract volts vector time =.005*[1:length(voltage)];%Create time vector plot (time, voltage)%Plot volts vs time xlabel ('Time in Seconds')% Label x axis ylabel ('Voltage')% Label y axis title ('Bike Strain Gage Voltage vs Time') grid %Put a grid on graph

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 4Winter Quarter MATLAB Script Files The preceding file is executed by issuing a MATLAB command: >> g13 This single command causes MATLAB to look in the current directory, and if a file g13.m is found, open it and execute all of the commands. The result, in this case, is a plot of the data from g13.dat. If MATLAB cannot find the file in the current working directory, an error message will appear.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 5Winter Quarter MATLAB Script Files When the file is not in the current working directory, a cd or chdir command may be issued to change the directory. >> cd a:\ % Make a:\ the current working directory >> g13

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 6Winter Quarter MATLAB Demo %MATLAB Example use of ginput clear, clf, hold off axis ([0, 10, 0, 10]) hold on plot ([1, 2, 2, 1, 1],[2, 2, 3, 3, 2]) text (1, 1.6, 'Click inside box to quit') while 1 [x, y, buttun] = ginput (1) if buttun == 1, plot (x, y, '+r'), end if buttun == 2, plot (x, y, 'oy'), end if buttun == 3, plot (x, y, '*g'), end if x > 1 & x 2 & y < 3, break; end end hold off

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 7Winter Quarter MATLAB Function Files A MATLAB function file (called an M-file) is a text (plain ASCII) file that contains a MATLAB function and, optionally, comments. The file is saved with the function name and the usual MATLAB script file extension, ".m". A MATLAB function may be called from the command line or from any other M-file.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 8Winter Quarter MATLAB Function Files When the function is called in MATLAB, the file is accessed, the function is executed, and control is returned to the MATLAB workspace. Since the function is not part of the MATLAB workspace, its variables and their values are not known after control is returned. Any values to be returned must be specified in the function syntax.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 9Winter Quarter MATLAB Function Files The syntax for a MATLAB function definition is: function [val1, …, valn] = myfunc (arg1, …, argk) where val1 through valn are the specified returned values from the function and arg1 through argk are the values sent to the function. Since variables are local in MATLAB (as they are in C), the function has its own memory locations for all of the variables and only the values (not their addresses) are passed between the MATLAB workspace and the function.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 10Winter Quarter MATLAB Function Files It is OK to use the same variable names in the returned value list as in the argument. The effect is to assign new values to those variables. As an example, the following slide shows a function that swaps two values.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 11Winter Quarter Example of a MATLAB Function File function [ a, b ] = swap ( a, b ) % The function swap receives two values, swaps them, % and returns the result. The syntax for the call is % [a, b] = swap (a, b) where the a and b in the ( ) are the % values sent to the function and the a and b in the [ ] are % returned values which are assigned to corresponding % variables in your program. temp=a; a=b; b=temp;

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 12Winter Quarter Example of a MATLAB Function File To use the function a MATLAB program could assign values to two variables (the names do not have to be a and b) and then call the function to swap them. For instance the MATLAB commands: >> x = 5 ; y = 6 ; [ x, y ] = swap ( x, y ) result in: x = 6 y = 5

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 13Winter Quarter MATLAB Function Files Referring to the function, the comments immediately following the function definition statement are the "help" for the function. The MATLAB command: >>help swap %displays: The function swap receives two values, swaps them, and returns the result. The syntax for the call is [a, b] = swap (a, b) where the a and b in the ( ) are the values sent to the function and the a and b in the [ ] are returned values which are assigned to corresponding variables in your program.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 14Winter Quarter MATLAB Function Files The MATLAB function must be in the current working directory. If it is not, the directory must be changed before calling the function. If MATLAB cannot find the function or its syntax does not match the function call, an error message will appear. Failure to change directories often results in the error message: Undefined function or improper matrix assignment.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 15Winter Quarter MATLAB Function Files When the function file is not in the current working directory, a cd or chdir command may be issued to change the directory. >> cd a:\ % Make a:\ the current working directory

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 16Winter Quarter MATLAB Function Files Unlike C, a MATLAB variable does not have to be declared before being used, and its data type can be changed by assigning a new value to it. For example, the function factorial ( ) on the next slide returns an integer when a positive value is sent to it as an argument, but returns a character string if the argument is negative.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 17Winter Quarter MATLAB Function Files function [n] = factorial (k) % The function [n] = factorial(k) calculates and % returns the value of k factorial. If k is negative, % an error message is returned. if (k < 0) n = 'Error, negative argument'; elseif k<2 n=1; else n = 1; for j = [2:k] n = n * j; end end