1 / 18

More Script Files in MATLAB

More Script Files in MATLAB. Script File I/O : Chapter 4. Global Variables The Input Command The Disp Command The fprintf Command. 81. Global Variables. Command window and script file variables are Global Variables

topper
Download Presentation

More Script Files in MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More Script Files in MATLAB Script File I/O : Chapter 4 • Global Variables • The Input Command • The Disp Command • The fprintf Command

  2. 81 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).

  3. 81- 84 INPUT TO A SCRIPT FILE 3 ways to input data to a script file: • 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 • Define variable in command window (global variable) To change variable: • Type new value for variable in command window • Execute script file

  4. 81- 84 INPUT TO A SCRIPT FILE • Interactively with user To change variable: • Execute script file • Enter new value when prompted This third option is done by using the input() function: x = input(‘text’) string 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. Please enter a value for x: 5 x = 5 User Input Interactive Example For example: (script file) Note the space: it makes the command window more readable x = input(‘Please enter a value for x: ’) (command window)

  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. 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. 84- 87 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

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

  10. 85 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) Note ; Display empty line Display Text Display the value of the variable ave_points

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

  12. 86 CREATE AND DISPLAY A TABLE How would you create the table format of data as shown in this slide? YEAR POPULATION (MILLIONS) 1984 127 1986 130 1988 136 1990 145 1992 158 1994 178 1996 211 Heading (first line) Heading (second line) Empty Line The array tableYP

  13. 86 CREATE AND DISPLAY A TABLE The year and population data is entered in two row vectors yr = [1984: 2: 1996]; pop = [127 130 136 145 158 178 211]; tableYP(:, 1) = yr’; tableYP(:, 2) = pop’; disp(‘ Year Population’) disp(‘ (millions)’) disp(‘ ‘) disp(tableYP) 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

  14. 87- 94 The fprintf command 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')

  15. 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) 5 is the number of spaces on the page to reserve for the number 2 is the number of digits after the decimal point f is the number format(e,E,g,G,i) fprintf ('text %-5.2f more text', variable_name)

  16. 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! fprintf ('text … %f more text … %g even more text … %e last text‘, var_1,var_2,var_3)

  17. The fprintf command 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')

  18. The fprintf command 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')

More Related