Word

advertisement
ENGR 1187 | MATLAB 05: Input and Output
Preparation Material
Learning Objectives
1. Create a basic program that can be used over and over or given to another person to use
2. Demonstrate proper use of the input command, which is the simplest command for
interacting with the user
3. Use fprintf, which allows the student control over how results are displayed in the
command window
Topics
Read Chapter 4 of the MATLAB book before coming to class. This preparation material is
provided to supplement this reading.
Students will learn a more detailed understanding of script files in MATLAB. This includes an
understanding of how global variables work, and then being to learn the basics of input and output
(I/O) commands. This material contains the following:
1.
2.
3.
4.
5.
Key Definitions
Global Variables
The input() command
The disp() command
The fprintf() command
1. Key Definitions
Command Window – MATLAB’s main window that opens when MATLAB is started. Script files are
executed in the window, or commands can be entered directly here.
Script File – a list of MATLAB commands, called a program that is saved in a file. When executed
(run), MATLAB executes the saved commands.
MATLAB Workspace – consists of the set of variables (named arrays) that are defined and stored
during a MATLAB session.
2. Global Variables
The command window and script file variables are Global Variables.

Global variables are variables that, once created in one part of MATLAB (e.g. command
window), are recognized and valid in other parts of MATLAB (e.g. script file).
1
ENGR 1181 MATLAB 05: Input and Output
Preparation Material

If a variable ‘x’ is defined in the command window, then it is also recognized in the script
file. The variable name and value remains in the script file until the value is re-assigned, or
the variables are cleared.
There are 3 ways to input data to a script file:
1. Define variable in script file (hardwired or hardcoded)
To change a variable:
 Edit script file, and change the value of the variable
 Save script file
 Execute script file
2. Define variable in command window (global variable)
To change a variable:
 Type new value for variable in command window
 Execute script file
NOTE: This only works if the script file doesn’t overwrite the variable.
3. Define interactively with user
To change a variable:
 Execute script file
 Enter new value when prompted
3. The input() command
This third option is done by using the input() function in the script file, which is then executed and
completed in the command window:
The script file would have the following input, where ‘text’ can be used to ask the user for an input:
x = input(‘text’)
string
The input() function will:


Display the string ‘text’ in the command window and wait.
Assign what is entered in the command window to the variable x.
2
ENGR 1181 MATLAB 05: Input and Output
Preparation Material
For example, enter the following in the Script File:
x = input(‘Please enter a value for x: ’)
Note the space between the colon and apostrophe.
It makes the command window more readable.
…and the following will execute in the Command Window:
Please enter a value for x:
Once the user enters ‘5’ and hits ‘Enter’, the Command Window will show:
Please enter a value for x: 5
x= 5
User Input
The user can also enter any valid MATLAB statement in response to the input() command. It can be
a variable, a vector, a matrix, an equation, or a string.
Please enter a value for x: 45
Please enter a value for x: [32 7 22]
Please enter a value for x: [4 8 ; 12 16 ; 20 24]
B=5; A=3;
Please enter a value for x: A*B
Please enter a value for x: ‘Ohio Stadium’
3
ENGR 1181 MATLAB 05: Input and Output
Preparation Material
The ‘s’ option
As you have seen, text typed into the command window in response to an input command without
single quotes to indicate a string will be interpreted as a variable name. The 's' option, tells the
input function to interpret whatever is typed as a string, i.e.
x = input('Please enter a value for x: ', 's')
Thus, the user does not need to even know what a string is. Adding modifiers as a single character
is common in MATLAB.
Output from a Script File
When a script file runs, output that is generated is displayed in the Command Window.

Output is displayed automatically if a statement does not end with a semicolon. Many times
you don’t want all outputs to be displayed in the command window.

Output can also be displayed intentionally by using the disp() command
4. The disp() command
NOTE: disp() adds a ‘line feed’ (put cursor at start of next line) to the end of its input. Therefore,
any following output appears on a new line.
4
ENGR 1181 MATLAB 05: Input and Output
Preparation Material
5. The fprintf() command
In it’s simplest form, fprintf() looks a lot like disp(), with control characters.
Control Characters include:
\n
starts a new line
\b
backspace
\t
horizontal tab
Unlike disp(), fprintf() does not automatically add a ‘line feed’. Therefore, the programmer
must include a \n at the end of every line.
fprintf ('text %-5.2f more text', variable_name)
% marks where to insert a number
-
is used as a flag to left justify your column
other choices include ‘+’ to add a ‘+’ or ‘-‘ before each variable, or
0 to fill in all gaps with 0s
5
is the number of spaces on the screen to reserve for the number
2
is the number of digits after the decimal point
f
is the number format (could also be: e, E, g, G, i)
fprintf ('text … %f more text … %g
even more text … %e last text',
var_1,var_2,var_3)
More than one variable can be printed at a time with fprintf().
Control characters can be used to help arrange appearance.
Without ‘\n’, subsequent fprintf() commands will not start on a new line!
fprintf ('header line 1 \n header line 2 \n')
fprintf (' %5i
%5.2f \n', tableYP')
5
ENGR 1181 MATLAB 05: Input and Output
Preparation Material
Tables can be printed with more control over spacing and how numbers appear
If there are more numbers to be printed than described in the 'text' portion of an fprintf()
statement, then the statement will be used over and over until all numbers are displayed.
fprintf ('header line 1 \n header line 2 \n')
fprintf (' %5i
%5.2f \n', tableYP')
The 'text' portion of the data fprintf() command should describe the data line layout.
Arrays are displayed column by column. If an array is arranged the way you want a table to appear,
as with tableYP in the previous disp example, then you need to transpose it for the fprintf()
command!
6
Download