Lecture 5 Input and Output input fprintf 2007

  • Slides: 17
Download presentation
Lecture 5 Input and Output input fprintf © 2007 Daniel Valentine. All rights reserved.

Lecture 5 Input and Output input fprintf © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Input n Some options: – Assign values to variables in a command line using

Input n 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 n General format: variable = input('Please enter a value: ') n The

Input function n General format: variable = input('Please enter a value: ') n The display in the Command window: Please enter a value: n Type a value after the displayed prompt; the variable now has been assigned the value that was entered.

Hands on n Area of a circle: – Type this into an m-file: –

Hands on n Area of a circle: – Type this into an m-file: – Execute and see this display in Command Window: Please enter the radius: 5. 2 radius = 5. 2000 area = 84. 9487

Hands on Free falling object: n Type this into an m-file: n n Execute:

Hands on Free falling object: n Type this into an m-file: n n 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

Input strings of text n General format: string = input('Please enter your name: ',

Input strings of text n General format: string = input('Please enter your name: ', 's') n The display in the Command window: Please enter your name: n 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).

Output options n The display variables: – You can display the value of a

Output options n The display variables: – You can display the value of a variable in different ways. § Typing “variable =” in the command window x = 500 § Using the fprintf command: – Type into an m-file: – Executing it displays in the Command Window: The value of x is 500.

fprintf() n The fprintf() command is one way to display the value of a

fprintf() n The fprintf() command is one way to display the value of a variable with a label. n 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() n To print a variable in your display on the command

Placeholders in fprintf() n 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). n %d: integer notation n %f: fixed point (decimal) notation – Most commonly used placeholder n %e: exponential notation n %g: whichever is shorter, %f or %e

Hands on n Type into an m-file: n Executing it displays in the command

Hands on n Type into an m-file: n Executing it displays in the command window: smiles = 7 Sarah smiles 7 times a day.

Hands on n Type into an m-file: n Executing it displays in Command Window:

Hands on n Type into an m-file: n 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!!

Format n When fprintf is used consecutively, MATLAB prints the results on the same

Format n When fprintf is used consecutively, MATLAB prints the results on the same line in the command window. – After the second execution of this command: Sarah smiles 7 times a day. >>

The n command n This command is a linefeed. It commands MATLAB to start

The n command n This command is a linefeed. It commands MATLAB to start on a new line so that sequential outputs are on separate lines. – Example Erin ate 4 waffles today. >> – Be sure to use n, not /n.

Width & precision fields n The width field specifies the minimum number of characters

Width & precision fields n The width field specifies the minimum number of characters to be printed. n The precision field specifies the number of those characters that will show up after the decimal point.

Hands on n %8. 2 f specifies that there can be no less than

Hands on n %8. 2 f 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. The weight is 57638. 75 pounds – Notice the blank spaces in the second output. – Use %% to have a % sign show up in output.

Exercises n Write a program tomodel a spring-mass system. The spring constant k (N/m),

Exercises n Write a program tomodel a spring-mass system. The spring constant k (N/m), the mass m (kg), amplitude xo (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]. n The formula to use is: § x=xocos(ωt), where ω=sqrt(k/m)

Summary I/O input – variable = input('Please enter a value: ') – string =

Summary I/O input – variable = input('Please enter a value: ') – string = input('Please enter a value: ', 's') n fprintf – fprintf('format-string', variable) n – – – %f = fixed point (decimal) notation %e = exponential notation %g = whichever is shorter, %f or %e