Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.

Slides:



Advertisements
Similar presentations
Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Advertisements

Input and Output ENGR 1181 MATLAB 5. Input and Output In The Real World Script files (which provide outputs given inputs) are important tools in MATLAB.
Al-Amer An Introduction to MATLAB Lesson 2: M-files Dr. Samir Al-Amer Term 061.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
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.
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
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.
Digital Image Processing Lecture3: Introduction to MATLAB.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
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.
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
SAS Workshop Lecture 1 Lecturer: Annie N. Simpson, MSc.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 4.
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.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (3): MATLAB Environment (Chapter 1)
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.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
1 The EDIT Program The Edit program is a full screen text editor that allows you to: Create text files Create text files Edit an existing text files Edit.
Plotting in MATLAB Rachel Davies Brittany Earnest Greg Fannon Nicholas Millan Khurram Najeeb.
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
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)
Script M-Files Group of Matlab commands placed in a text file with a text editor. Matlab can open and execute the commands exactly as if they were entered.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Scientific Computing Introduction to Matlab Programming.
Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.
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 &
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Introduction to Engineering MATLAB – 7 Script Files - 2 Agenda Script files continued.
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
CSE123 Lecture 3 Files and File ManagementScripts.
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.
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.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
ENG 1181 College of Engineering Engineering Education Innovation Center 1 Script File Input – Output : Chapter 4 PLEASE HAVE STUDENTS START MATLAB NOW.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
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.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
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.
MySQL Getting Started BCIS 3680 Enterprise Programming.
MATLAB – More Script Files
Release Numbers MATLAB is updated regularly
Introduction to Matlab
Basic operations in Matlab
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Using Script Files and Managing Data
Scripts In Matlab.
Input and Output Python3 Beginner #3.
Presentation transcript:

Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files

SCRIPT FILE  A script file is a sequence of MATLAB commands, called a program.  The file can be edited and saved.  When the file is run, MATLAB executes the commands in the order they are written just as if they were typed in the Command Window.  Using a script file is convenient since it can be executed many times without the need to retype the commands (as is needed when working in the Command Window).  Script files are also called M-files because the extension.m is used when they are saved.

In-Class Exercise Give an everyday example of when someone would use a script file. Example: Calculating the class average on an exam.

CREATING A SCRIPT FILE In the command window click on the File menu, select New, and then select M-file. Once M-file is selected, the M-file Editor/Debugger window opens.

The M-file Editor/Debugger window The commands of the script file are typed line by line.

EXAMPLE OF A SCRIPT FILE The text can be typed in this window, or it can be typed (and edited) in any text editor (e.g Microsoft Word) and then pasted here. Recall that typing % at the beginning of a line designates the line as a comment, which is not executed.

SAVING A SCRIPT FILE  Once the script file is completed, it must be saved. In our class use Save As and save in the floppy A drive.  The name of the script file follows the rules for names of variables in MATLAB. (Must begin with a letter, can include digits and underscore, up to 31 characters long, don’t give the file a name of a variable that is used, or a predefined variable, don’t use a name of a MATLAB command or a function.)  There is no need to add “.m” at the end of a file name. The software will automatically add it for you.

RUNNING A SCRIPT FILE  A script file is run from the command window.  To run a script file that is saved in drive A, the MATLAB search path has to be modified to include drive A, or the working directory has to be changed to drive A.  To change the working directory to drive A type (in the command window): cd a:  To run a script file type the name of the file (without the extension.m) in the command window.

EXAMPLE OF RUNNING A SCRIPT FILE The output that is generated when the script file runs is printed in the command window. command window Setting the working directory to drive A Type the name of the script file

GLOBAL VARIABLES  Global variables are variables that, once created in one part of MATLAB, are recognized in other parts of MATLAB.  Variables that are created in the command window are recognized and can be used in a script file.  Variables that are created in a script file are recognized and valid in the command window.

INPUT TO A SCRIPT FILE 1.The variable is defined in the script file. To run the script file with different variable value, the file is edited and the value of the variable is changed. Then the file is saved, and executed. 2.The variable is defined in the command window. To run the script file with a different value, a new value is given to the variable in the command window. Then the script file is executed. A script file is a program that can be executed with different values of its variables. This can be done in three different ways depending on where and how the variables are defined:

INPUT TO A SCRIPT FILE 3.The variable is defined in the script file without a specific value. When the script file runs the user is prompted to enter a value from the command window. This is done by using the input statement: x = input(‘text’) For example: x = input(‘Please enter a value for x’) Once a number (or a vector, or a matrix) is entered, x has this value. string

OUTPUT FROM A SCRIPT FILE  When a script file runs, output that is generated is displayed in the command window.  Output is displayed automatically if a statement does not end with a semicolon.  Output can also be displayed intentionally by using the disp command.

The disp COMMAND disp(A)Displays the content, but not the name, of the variable A. disp(‘text’)Displays the text (string) that is enclosed within the single quotes. string

EXAMPLE OF A SCRIPT FILE THAT USES THE input AND disp COMMANDS

RUNNING THE SCRIPT FILE WITH THE input AND disp COMMANDS IN THE COMMAND WINDOW The grades are entered following the prompt.

CREATE AND DISPLAY A TABLE % This script file shows how to create and display a table. % The table includes the population data from Lecture 2. yr = [ ]; % Creating a vector of year numbers. pop = [ ]; % Creating a vector of population data. table_yr_pop(:,1)=yr'; % Substituting the yr vector in the first column of the table matrix. table_yr_pop(:,2)=pop'; % Substituting the pop vector in the second column of the table matrix. disp(' YEAR POPULATION') % Display titles. disp(' (MILLIONS)') % Display titles. disp(' ') % Display an empty line. disp(table_yr_pop) % Display the table.

CREATE AND DISPLAY A TABLE >> Lecture4Example3 YEAR POPULATION (MILLIONS) Executing the script file from the previous slide in the command window gives: