Format IO

10/3/2009
Matlab®
Computing for Engineers and Scientists CITS1005
Matlab®
Computing for Engineers and Scientists CITS1005
Overview
• Example of a text file output
Lecture 19: Formatted File I/O
• Formatted I/O functions
– fprintf, sprintf, fscanf
• Reading lines from text files
• Processing strings
– Breaking a string into tokens
– Comparing strings for equality
– Converting strings to numerals
Matlab®
Computing for Engineers and Scientists CITS1005
Formatting input/output
•
Once a file is opened, you can write text to the file
using the fprintf function.
•
The general syntax of the fprintf function is:
fprintf(fid, 'format-control-string', value1,
value2, ...)
•
•
If fid is omitted, the formatted string is written to
the screen (i.e. a fid = 1 is assumed).
The argument list to fprintf has two parts:
1. The format-control-string, which is a string describing
how the data should be printed out.
2. A list of variables to be printed.
Matlab®
Computing for Engineers and Scientists CITS1005
Syntax of the conversion sequence
• The general form of a conversion sequence is:
%-w.pf
– This sequence means format the value using the
type specified by f with a minimum width of w
(extra spaces are added as necessary) and p
decimal places.
– The minus sign is a flag indicating alignment, to
the left if present; when omitted, align to the
right.
1
10/3/2009
Matlab®
Computing for Engineers and Scientists CITS1005
Example
•
The format string will contain special conversion sequences and escape characters. For
example, the command:
>> fprintf('She sells %d %s for $%.2f \n', 99, …
'sea shells', 4.95);
produces the following output:
She sells 99 sea shells for $4.95
– The first conversion sequence, %d, in the format string is replaced by the first
data value (99). The d part signifies that the value 99 should be displayed as
an integer.
– The second conversion sequence, %s, is an instruction to format the second
data value as a string of characters (the string 'sea shells').
– The last conversion sequence, %.2f, is an instruction to format the third
value as a floating point value. The .2 part signifies that two decimal places
should be printed.
– Finally the \n character is an escape sequence that represents a newline
character. After printing a newline character, the cursor jumps to the next
line and future printing occurs on this new line.
Matlab®
Computing for Engineers and Scientists CITS1005
Escape Characters
• The most commonly occurring escape
characters are:
\n
A newline character.
\t
A tab character.
\b
A backspace character.
\\
A single \ character.
''
A single ' character.
%%
A single % character.
Matlab®
Computing for Engineers and Scientists CITS1005
Common conversion sequences
• The most commonly occurring conversion
sequences are:
%d
Format the value as an integer.
%f
Format the value as a floating point value.
%e
Format the value as a floating point value
in exponential notation.
%s
Format the value as a character string.
%c
Format the value as a single character.
Matlab®
Computing for Engineers and Scientists CITS1005
Formatted Text - fprintf, sprintf and fscanf
• We have already introduced fprintf for
formatting strings, eg:
>> fprintf('She sells %d %s for $%.2f \n', 99,…
'sea shells', 4.95);
produces the following output:
She sells 99 sea shells for $4.95
• Another common use is for formatting file (or
printer) output for further use such as
aligning data in tables...
2
10/3/2009
Matlab®
Computing for Engineers and Scientists CITS1005
Formatting output to a text file
• Imagine a program that generates a 4x3 array of
random numbers and writes them out to a text file:
filename = 'outputfile.dat';
rows = 4;
cols = 3;
m = rand(rows, cols);
% Initialisation.
Matlab®
Formatting output to a text file (cont.)
fprintf(fid, 'This is a %dx%d matrix of random numbers.\n\n',
rows, cols);
for r = 1 : rows
fprintf(fid, 'Row %d: ', r);
% Write the row number
% into the file.
for c = 1 : cols
fprintf(fid, ' %8.4f', m(r,c));
% Write each column of the row.
end
% Generate random numbers.
[fid, message] = fopen(filename, 'wt');
% Open a text file for writing.
fprintf(fid, '\n');
if fid == -1
fprintf('File %s could not be opened \n', filename);
error(message);
end
Matlab®
Computing for Engineers and Scientists CITS1005
Computing for Engineers and Scientists CITS1005
% Write a newline before
% starting the next row.
end
fclose(fid);
Matlab®
The results
% Close the file.
Computing for Engineers and Scientists CITS1005
An alternative
• Matlab's implementation of the fprintf function is "vectorised" in that
if the variable to be formatted is an array, the format specification string is
re-used until all elements of the array have been printed.
• Files created by this program look like:
This is a 4x3 matrix of random numbers.
• Thus, the loop in the previous example could have be rewritten:
for r = 1 : rows
Row
Row
Row
Row
1:
2:
3:
4:
0.9218
0.7382
0.1763
0.4057
0.9355
0.9169
0.4103
0.8936
0.0579
0.3529
0.8132
0.0099
fprintf(fid, 'Row %d:
fprintf(fid, '
', r);
% Write the row number
% into the file.
%8.4f', m(r,:));
% Write each column of the row
% reusing the format string.
fprintf(fid, '\n');
% Write a newline before
% starting the next row.
end
3
10/3/2009
Matlab®
Computing for Engineers and Scientists CITS1005
The sprintf function
• Just like the fprintf function except that rather
than outputting the result to the screen, file or
printer, sprintf returns the string as the result of
the function.
• For example, the following command formats a
string and assigns the text output to a variable called
str:
>> str = sprintf('She sells %d %s for $%f \n', 99, 'sea shells', 4.95);
• We can construct filenames automatically using the
sprintf function to format a string...
Matlab®
Computing for Engineers and Scientists CITS1005
The fscanf function
• The opposite of the fprintf function - it reads a
formatted string and assigns values to variables.
• The syntax of the fscanf function is:
[val, count] = fscanf(fid, format-string)
– Reads text from the file associated with the file id fid.
– The variable format-string specifies how the data is to be
read.
– The data read in is assigned to the variable val as a column
vector.
– The number of elements successfully read in is returned in
the output variable count.
Matlab®
Computing for Engineers and Scientists CITS1005
Constructing filenames automatically
• For example, imagine opening an array of files named dataFile1.dat,
dataFile2.dat, dataFile3.dat, etc.
Nfiles = 3;
baseName = 'dataFile';
extension = '.dat';
for n = 1 : Nfiles
% Construct the filename.
filename = sprintf('%s%d%s', baseName, n, extension);
[fid(n), msg] = fopen(filename, 'r');
if fid(n) == -1
fprintf('Error opening %s\n%s\n', filename, msg);
return;
end
end
% fid is now an array of file id values ready for use.
Matlab®
Computing for Engineers and Scientists CITS1005
The fscanf function (cont.)
• If some file data is encountered that is not
consistent with the format specification, the
reading is immediately stopped.
• The fscanf function ignores any white space
characters (spaces, tabs, newlines, etc.) in
reading a file.
• The fscanf function is normally used in its
"vectorised" mode, with the format string
being reused until the file ends or inconsistent
file formatting is encountered.
4
10/3/2009
Matlab®
•
Matlab®
Computing for Engineers and Scientists CITS1005
Example
Specifying scanning size
For example, imagine a data file with the numbers 1 to 8 separated by
spaces, tabs, and newlines:
• One can also specify a size argument to the fscanf function which
specifies both how many values to read in and the "shape" of the matrix to
read the values into.
1 2
6
•
Computing for Engineers and Scientists CITS1005
3 4 5
7
• Thus, the general syntax of the fscanf function is:
8
The following command reads and displays the contents of the file:
>> a = fscanf(fid, '%f')
>> a =
1
2
3
4
5
6
7
8
Matlab®
%
%
%
%
Read the file as an
array of floats.
The result is a
column vector.
[val, count] = fscanf(fid, format-string, size)
• where size can be a 2D matrix that specifies how many values should be
read.
• For example:
>> a = fscanf(fid, '%f', [2, 4])
a =
1
2
3
4
5
6
% Read data in as a
% 2x4 matrix.
7
8
Computing for Engineers and Scientists CITS1005
Specifying scanning size (cont.)
>> a = fscanf(fid, '%f', 3)
% Read just 3 values.
a =
1
2
3
5