Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Lect05 Input and Output

The document discusses input and output in MATLAB. It covers using the input function to assign values to variables from user input and displaying output using fprintf formatting strings. Examples are provided to demonstrate taking user input and displaying output with labels.

Uploaded by

Khoa Nguyễn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lect05 Input and Output

The document discusses input and output in MATLAB. It covers using the input function to assign values to variables from user input and displaying output using fprintf formatting strings. Examples are provided to demonstrate taking user input and displaying output with labels.

Uploaded by

Khoa Nguyễn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Lecture 5

Input and Output

input

fprintf
Input
 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:
variable = input('Please enter a value: ')

 The display in the Command window:


Please enter a value:

 Type a value after the displayed prompt;


the variable now has been assigned the
value that was entered.
Hands on
 Area of a circle:
– Type this into an m-file:
radius = input('Please enter the radius: ')

area = pi*radius^2

– 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:
 Type this into an m-file:
height_0=input('Please enter the initial height (in meters): ');
time=input('Please enter the time the object was in the air (in seconds): ');
velocity_0=input('Please enter the initial velocity (in meters): ');
a=9.81;
height_f=height_0+velocity_0*time+(1/2)*a*time^2

 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
 General format:
string = input('Please enter your name: ', 's')

 The display in the Command window:


Please enter your name:

 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
 The display variables:
– You can display the value of a variable in
different ways.
 Typing “variable =” in the command window
x=500
x =
500
 Using the fprintf command:
– Type into an m-file:
x=500;
fprintf('The value of x is %f.\n',x)
– Executing it displays in the Command Window:
The value of x is 500.
fprintf()
 The fprintf() command is one way to
display the value of a variable with a label.

 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).
 %d: integer notation
 %f: fixed point (decimal) notation
– Most commonly used placeholder
 %e: exponential notation
 %g: whichever is shorter, %f or %e
Hands on
 Type into an m-file:
smiles=7
fprintf('Sarah smiles %d times a day.',smiles)

 Executing it displays in the command window:


smiles =

Sarah smiles 7 times a day.


Hands on
 Type into an m-file:
month=input('Please enter your month of birth (i.e. May): ', 's');
day=input('Please enter your number day of birth: ');
fprintf('Your birthday is %s %d!!',month,day)

 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
 When fprintf is used consecutively,
MATLAB prints the results on the same
line in the command window.
smiles=7;
fprintf('Sarah smiles %d times a day.',smiles)

– After the second execution of this command:


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.
– Example
waffles=4;
fprintf('Erin ate %d waffles today.\n',waffles)
Erin ate 4 waffles today.
>>

– Be sure to use \n, not /n.


Width & precision fields
 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.
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.
weight= 57638.75;
fprintf('The weight is %8.2f pounds \n', weight)
fprintf(‘The weight is %50.2f pounds \n’, weight)

The weight is 57638.75 pounds


The weight is 57638.75 pounds

– Notice the blank spaces in the second output.


– Use %% to have a % sign show up in output.
Exercises
 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].
 The formula to use is:
 x=xocos(ωt), where ω=sqrt(k/m)
Summary I/O
 input
– variable = input('Please enter a value: ')
– string = input('Please enter a value: ', 's')
 fprintf
– fprintf('format-string', variable)
– %f = fixed point (decimal) notation
– %e = exponential notation
– %g = whichever is shorter, %f or %e

You might also like