Download presentation
Presentation is loading. Please wait.
1
Lecture 5 Input and Output inputfprintf © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
2
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.
3
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:
4
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 = 5.2000 5.2000 area = 84.9487 84.9487
5
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 = 84.1450 84.1450
6
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:
7
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 = 500 500 The value of x is 500.
8
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.
9
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
10
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.
11
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!!
12
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.>>
13
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. >>
14
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.
15
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 57638.75 pounds
16
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)
17
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.