Lect05 Input and Output
Lect05 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: ')
area = pi*radius^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
height_f =
84.1450
Input strings of text
General format:
string = input('Please enter your name: ', 's')
General format:
MATLAB code:
fprintf('format-string', variable)