Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 2.The Input Command 3.The Disp Command 4.The fprintf Command

2 ENG 1181 2 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

3 ENG 1181 3 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 81- 84

4 ENG 1181 4 INPUT TO A SCRIPT FILE 3.Interactively with user To change variable:  Execute script file  Enter new value when prompted x = input(‘text’) string 81- 84 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.

5 ENG 1181 5 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

6 ENG 1181 6 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

7 ENG 1181 7 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 ')

8 ENG 1181 8 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 84- 87

9 ENG 1181 9 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. 84- 86

10 ENG 1181 10 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 ;

11 ENG 1181 11 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

12 ENG 1181 12 CREATE AND DISPLAY A TABLE YEAR POPULATION (MILLIONS) 1984 127 1986 130 1988 136 1990 145 1992 158 1994 178 1996 211 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

13 ENG 1181 13 CREATE AND DISPLAY A TABLE yr = [1984: 2: 1996]; pop = [127 130 136 145 158 178 211]; 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

14 ENG 1181 14 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 87- 94

15 ENG 1181 15 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)

16 ENG 1181 16 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!

17 ENG 1181 17 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

18 ENG 1181 18 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


Download ppt "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."

Similar presentations


Ads by Google