ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.

Slides:



Advertisements
Similar presentations
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.
Advertisements

7 Copyright © Oracle Corporation, All rights reserved. Producing Readable Output with i SQL*Plus.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Lecture 5 Input and Output inputfprintf © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Pasewark & Pasewark Microsoft Office XP: Introductory Course 1 INTRODUCTORY MICROSOFT WORD Lesson 7 – Working With Documents.
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.
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.
AN ENGINEER’S GUIDE TO MATLAB
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.
Chapter 5 Review: Plotting Introduction to MATLAB 7 Engineering 161.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Web Technologies Website Development Trade & Industrial Education
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (3): MATLAB Environment (Chapter 1)
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Asking the USER for values to use in a software 1 Input.
Introduction to Engineering MATLAB – 7 Script Files - 2 Agenda Script files continued.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
ENG 1181 College of Engineering Engineering Education Innovation Center P. 1 MAT - Conditional Statements Topics Covered: 1. if based conditional statements.
GENERATION OF RANDOM NUMBERS
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.
Chapter 14 Formatting Readable Output. Chapter Objectives  Add a column heading with a line break to a report  Format the appearance of numeric data.
ENG College of Engineering Engineering Education Innovation Center 1 Basic For Loops in MATLAB Programming in MATLAB / Chapter 6 Topics Covered:
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.
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.
CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 2 in MATLAB Topics Covered: 1.Functions in Script Files Inline Functions.
Overview Input Input statements Menu statements Output Display statements fprintf statements Message boxes.
A Guide to SQL, Sixth Edition 1 Chapter 7 Reports.
Structured Computer Programming EE 201 Introduction to MATLAB 7 for Engineers بسم الله الرحمن الرحيم King Abdulaziz University College of Engineering Dept.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
ENG College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices.
MATLAB – More Script Files
Introduction to Programming for Mechanical Engineers (ME 319)
Intro to PHP & Variables
INPUT & OUTPUT scanf & printf.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Chapter 3 Input output.
Islamic University of Gaza
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Experiment No. (1) - an introduction to MATLAB
Using Script Files and Managing Data
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Presentation transcript:

ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables 2.The Input Command 3.The Disp Command 4.The fprintf Command

ENG Global Variables  Command window and script file variables are Global Variables  Global variables are variables that, once created in one part of MATLAB (e.g. command window), are recognized and valid in other parts of MATLAB (e.g. script file). 81

ENG INPUT TO A SCRIPT FILE 3 ways to input data to a script file: 1.Define variable in script file (hardwired or hardcoded) To change variable:  Edit script file, and change the value of the variable  Save script file  Execute script file 2.Define variable in command window (global variable) To change variable:  Type new value for variable in command window  Execute script file

ENG INPUT TO A SCRIPT FILE 3.Interactively with user To change variable:  Execute script file  Enter new value when prompted x = input(‘text’) string This third option is done by using the input() function: The input function will:  display the string ‘text’ in the command window and wait.  assign what is entered in the command window to the variable x.

ENG Interactive Example For example: (script file) x = input(‘Please enter a value for x: ’) Please enter a value for x: 5 x = 5 User Input (command window) Note the space: it makes the command window more readable

ENG Input Statement Examples Given the MATLAB command, what is typed in the command window can be any valid MATLAB statement. x = input('Please enter a value for x: ') Please enter a value for x: 'String' Please enter a value for x: [1 2 3] B=5; Please enter a value for x: B A=3; Please enter a value for x: A*B

ENG The 's' option As you have seen, text typed into the command window in response to an input command without single quotes to indicate a string will be interpreted as a variable name. The 's' option, tells the input function to interpret whatever is typed as a string, i.e. Thus the user does not need to even know what a string is. Adding modifiers as a single character is common in MATLAB. x = input('Please enter a value for x: ', ' s ')

ENG 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. Many times you don’t want all outputs to be displayed in the command window.  Output can also be displayed intentionally by using the disp() command

ENG THE disp() COMMAND disp(A) disp(‘text’) string disp() adds a ‘line feed’ to the end of it’s input. So, any following output appears on a new line. Displays the value of the variable A. Displays the text (string) that is enclosed within the single quotes

ENG SCRIPT FILE INPUT/OUPUT Chapter4Example5 Script file %This scipt file calculates the average points scored %in three games. The points from each game are assigned %to the variable by using the input() command. %The disp() command is used to display the output game1 = input(‘Enter the points scored in the first game‘); game2 = input(‘Enter the points scored in the second game‘); game3 = input(‘Enter the points scored in the third game‘); ave_points = (game1 + game2 + game3) / 3; disp(‘ ‘) disp(‘The average points scored in a game is:’) disp(‘ ‘) disp(ave_points) Display empty line Display Text Display the value of the variable ave_points 85 Note ;

ENG EXAMPLE OF RUNNING A SCRIPT FILE WITH INPUT/OUPUT Command Window: >> Chapter4Example5 Enter the points scored in the first game89 Enter the points scored in the second game60 Enter the points scored in the third game82 The average points scored in a game is: 77 >> The scores are entered following the prompt Display empty line Display text Display the values of variable ave_points 85

ENG CREATE AND DISPLAY A TABLE YEAR POPULATION (MILLIONS) How would you create the table format of data as shown in this slide? Heading (first line) Heading (second line) Empty Line The array tableYP 86

ENG CREATE AND DISPLAY A TABLE yr = [1984: 2: 1996]; pop = [ ]; tableYP(:, 1) = yr’; tableYP(:, 2) = pop’; disp(‘YearPopulation’) disp(‘(millions)’) disp(‘ ‘) disp(tableYP) The year and population data is entered in two row vectors yr is entered as the first column in array tableYP pop is entered as the second column Display heading (first line) Display heading (second line) Display an empty line. Display the array tableYP 86

ENG In it’s simplest form, fprintf looks a lot like disp, with control characters Control Characters: \n starts a new line \b backspace \t horizontal tab fprintf does not automatically add a ‘line feed’ you must include a \n fprintf ('text typed as a string \n') The fprintf command

ENG fprintf ('text %-5.2f more text', variable_name) The fprintf command % marks where to insert a number -is a flag to left justify (other choices + to add a sign, 0 pads with 0’s) 5is the number of spaces on the page to reserve for the number 2is the number of digits after the decimal point fis the number format(e,E,g,G,i)

ENG fprintf ('text … %f more text … %g even more text … %e last text‘, var_1,var_2,var_3) The fprintf command More than one variable can be printed Control characters can be used to help arrange appearance Without a \n, subsequent fprintf commands will not start on a new line!

ENG Tables can be printed with more control over spacing and how numbers appear If there are more numbers to be printed than described in the 'text' portion of an fprintf statement, then the statement will be used over and over until all numbers are displayed. fprintf ('header line 1 \n header line 2 \n') fprintf (' %5i %5.2f \n', tableYP') The fprintf command

ENG The 'text' portion of the data fprintf should describe the data line layout Arrays are displayed column by column. If an array is arranged the way you want a table to appear, as with tableYP in the previous disp example, then you need to transpose it for fprintf! fprintf ('header line 1 \n header line 2 \n') fprintf (' %5i %5.2f \n', tableYP') The fprintf command