Lecture 5 Input and Output inputfprintf © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Input Some options: Some options: –Assign values to variables in a command line using the equal ( = ) sign. –Use the input function for interactive assignment of values to some of the variables within your computer program.
Input function General format: General format: The display in the Command window: The display in the Command window: Type a value after the displayed prompt; the variable now has been assigned the value that was entered. Type a value after the displayed prompt; the variable now has been assigned the value that was entered. variable = input('Please enter a value: ') Please enter a value:
Area of a circle: Area of a circle: –Type this into an m-file: –Execute and see this display in Command Window: Hands on Please enter the radius: 5.2 radius = area =
Hands on Free falling object: Free falling object: Type this into an m-file: Type this into an m-file: Execute: See this display in Command Window: Execute: See this display in Command Window: Please enter the initial height (in meters): 25 Please enter the time the object was in the air (in seconds): 3 Please enter the initial velocity (in meters): 5 height_f =
Input strings of text General format: General format: The display in the Command window: The display in the Command window: This is used when the input is a string (e.g., names, months, etc.). The variable, in this case, is a ‘char’ class (or type). This is used when the input is a string (e.g., names, months, etc.). The variable, in this case, is a ‘char’ class (or type). string = input('Please enter your name: ', 's') Please enter your name:
The display variables: The display variables: –You can display the value of a variable in different ways. Typing “variable =” in the command window Using the fprintf command: –Type into an m-file: –Executing it displays in the Command Window: Output options x = The value of x is 500.
fprintf() The fprintf() command is one way to display the value of a variable with a label. The fprintf() command is one way to display the value of a variable with a label. General format: General format: MATLAB code: fprintf('format-string', variable) Note: You can also use the disp() function to display results. See the help on disp() and the examples in the text.
Placeholders in fprintf() To print a variable in your display on the command window, its place must be indicted by % in the format-string followed by the format of presentation ( d, f, e, g ). To print a variable in your display on the command window, its place must be indicted by % in the format-string followed by the format of presentation ( d, f, e, g ). %d : integer notation %d : integer notation %f : fixed point (decimal) notation %f : fixed point (decimal) notation –Most commonly used placeholder %e : exponential notation %e : exponential notation %g : whichever is shorter, %f or %e %g : whichever is shorter, %f or %e
Hands on Type into an m-file: Type into an m-file: Executing it displays in the command window: Executing it displays in the command window: smiles = 7 Sarah smiles 7 times a day.
Hands on Type into an m-file: Type into an m-file: Executing it displays in Command Window: Executing it displays in Command Window: Please enter your number month of birth (i.e. May): August Please enter your number day of birth: 11 Your birthday is August 11!!
When fprintf is used consecutively, MATLAB prints the results on the same line in the command window. When fprintf is used consecutively, MATLAB prints the results on the same line in the command window. –After the second execution of this command: Format Sarah smiles 7 times a day.Sarah smiles 7 times a day.>>
The \n command This command is a linefeed. It commands MATLAB to start on a new line so that sequential outputs are on separate lines. This command is a linefeed. It commands MATLAB to start on a new line so that sequential outputs are on separate lines. –Example –Be sure to use \n, not /n. Erin ate 4 waffles today. >>
Width & precision fields The width field specifies the minimum number of characters to be printed. The width field specifies the minimum number of characters to be printed. The precision field specifies the number of those characters that will show up after the decimal point. The precision field specifies the number of those characters that will show up after the decimal point.
Hands on %8.2f specifies that there can be no less than 8 characters in the displayed output, and that two of them are to follow the decimal point. %8.2f specifies that there can be no less than 8 characters in the displayed output, and that two of them are to follow the decimal point. –Notice the blank spaces in the second output. –Use % to have a % sign show up in output. The weight is pounds
Exercises Write a program tomodel a spring-mass system. The spring constant k (N/m), the mass m (kg), amplitude x o (m), and time, t, elapsed (s) are the input. Display output [i.e., dispay the displacement x in meters of the system at the given conditions]. Write a program tomodel a spring-mass system. The spring constant k (N/m), the mass m (kg), amplitude x o (m), and time, t, elapsed (s) are the input. Display output [i.e., dispay the displacement x in meters of the system at the given conditions]. The formula to use is: The formula to use is: x=x o cos(ωt), where ω=sqrt(k/m)
Summary I/O input input –variable = input('Please enter a value: ') –string = input('Please enter a value: ', 's') fprintf fprintf –fprintf('format-string', variable) –%f = fixed point (decimal) notation –%e = exponential notation –%g = whichever is shorter, %f or %e