Materials Files-642917 Lecture8Inputandoutput
Materials Files-642917 Lecture8Inputandoutput
1
Objectives
2
Strings
3
Strings
>> n = 'Raritan Valley' >> a = 'apple';
n = >> b = 'banana';
Raritan Valley >> c = 'canteloupe';
4
Strings and numbers
MATLAB doesn't allow you to mix numbers and strings without
converting the numbers to a string first. num2str(x) converts the
number(s) in x to a string.
>> x = pi;
>> disp(['Pi is approximately ', num2str(x)])
Pi is approximately 3.1416
5
The input command
6
The input command
7
The input command
8
The input command
MATLAB will automatically detect the kind of input based on how it is
formatted:
◦ Scalars – a single number
◦ Matrices – a list of numbers in square brackets
◦ Strings – characters in single quotes
◦ An expression to calculate – characters not in quotes
9
Graphical input
10
Output
>> x = magic(3);
>> disp(x)
8 1 6
3 5 7
disp(x) displays the contents
of the variable x. 4 9 2
11
Formatted output
Sometimes the output from disp isn't adequate, or you want to display
numbers in a specific format.
fprintf (formatted print function) allows you to control the
formatting of numbers in output and automatically print multiple lines
of output.
fprint('format string', var1, var2...)
The format string is a template that describes how the output will
look, using placeholders that MATLAB will fill with the variables.
12
Formatted output
>> x = 1.2345;
>> fprintf('The answer is %f\n', x)
The answer is 1.234500
%f is a placeholder. MATLAB puts the contents of x where %f is in the
string.
\n tells MATLAB to move to the next line after printing the output. You
should always finish the format string with \n, or else all the output will
run together on the same line.
13
Formatted output
14
Formatted output
If there are more variables than placeholders, MATLAB will re-use the
format string until all the variables have been used.
15
Formatted output
If the provided variable is a matrix, MATLAB will print a line of output
for every entry in the matrix, starting in the upper left and going down
the columns.
>> x = 5:-1:1
x =
5 4 3 2 1
>> fprintf('T minus %d seconds\n', x)
T minus 5 seconds
T minus 4 seconds
T minus 3 seconds
T minus 2 seconds
T minus 1 seconds
16
Types of placeholders
17
Types of placeholders
18
Types of placeholders
You can specify the maximum length of the number and the number
of digits after the decimal point/number of significant figures:
◦ %8.2f is a placeholder up to 8 characters wide that will display a number
with 2 digits after the decimal point.
◦ %8.3g is a placeholder for a number in scientific notation 8 characters long,
with 3 significant figures.
19
Importing data
The Import Wizard (found in File > Import Data) is the easiest way to
retrieve data from external files.
Example file formats MATLAB can import:
◦ Text: .dat, .txt, .csv
◦ Spreadsheets: .xls, .xlsx, .wk1
◦ Images: .bmp, .jpg, .gif
◦ Sound: .wav
◦ Video: .avi, .mpg
20
Import Wizard
21
Import Wizard
22
Commands to import and
export data
For information about how to import and export specific types of files
within scripts or functions without using the Import Wizard, read help
fileformats to look up the correct command, then read the manual
entry on the command you want.
23
Next week
24