220 likes | 421 Views
Input/Output and Plotting Basics. Material from Chapter 2.6-2.7 and 2.11Input deals with things a user tells the computerOutput deals with things the computer tells the user. Keyboard Input. MATLAB allows you to prompt" for input interactivelyThis is through the input function:input(prompt_
E N D
1. CS 101 – 010
Lecturer: Daniel J. Boston
GITC 2315-C
wednesday friday
1:00-2:30pm 2:30-4pm Computer Programming andProblem SolvingInput/Output and Plotting Basics
2. Input/Output and Plotting Basics Material from Chapter 2.6-2.7 and 2.11
Input deals with things a user tells the computer
Output deals with things the computer tells the user
3. Keyboard Input MATLAB allows you to “prompt” for input interactively
This is through the input function:
input(prompt_string)
input(prompt_string, options_string)
prompt_string tells MATLAB what text to display while waiting for a user to type something
options_string lets you specify how MATLAB should interpret the input
Use ‘s’ to indicate the input is a string
4. Keyboard Input Try it out:
user_name = input(‘What is your name? \n’, ‘s’)
user_age = input(‘What is your age? \n’)
5. Controlling Output MATLAB gives you a number of ways to control how output is displayed
These controls range from simple to complex
Note that changing how numbers are displayed does not change the precision of the underlying computations
To change the default precision of echoed numbers in the Command Window, use the format command
6. format Options
7. disp function The disp function outputs array values to the Command Window
This is often used to display strings (which are character arrays)
Quickly convert numbers to strings by using num2str or int2str
Example:
str = [‘pi:’ num2str(pi) ‘ pi rounded:’ int2str(pi)];
disp(str)
8. fprintf function fprintf can be used to control how output is displayed
fprintf(format,data)
fprintf(format,data1,data2, …)
format is a string specifying how to display the data
data, datax are variables or values (either scalar or array)
Example:
fprintf(‘pi: %d pi rounded: %1.0f \n’,pi,pi)
9. fprintf function fprintf is not perfect; it only shows the “real” portion of complex numbers
num2str with disp will show both real and complex parts of a number
If you are displaying complex numbers, use disp
10. fprintf function The %d characters in the format string are called “conversion characters”
Each conversion character is a data place-holder in the format string
Each conversion character has a number of optional formatting parameters
These parameters go between the % and the conversion character (d, f, i, etc.)
11. fprintf function