# 1. Writing Text Files

## 1.1. High-Level ASCII Write

The function save can be used to write a matrix to an ASCII file.

If you forget to include the variable name to save in the following examples (after the name of the file), MATLAB will save all variables in your workspace to the file.

In the following, the contents of the file can be displayed in the command window using the function type or the file can be opened using a text editor (Notepad, TextEdit, GEdit) after finding the file using a file browser.

Array Examples:

A = [1:10]; % Array to save
% save using default formatting.
% Don't forget the variable name to save, A, at the end.
save -ascii file1.txt A
type file1.txt % Display file contents in command window.

A = [1:10]; % Array to save
save -ascii -double file2.txt A % Save numbers with 16 decimal places.
type file2.txt % Display file contents in command window.


Matrix Example:

M = rand(5); % 5x5 random matrix to save.
save -ascii file3.txt M % Use default formatting
type file3.txt % Display file contents in command window.

M = rand(5); % 5x5 random matrix to save.
save -ascii -double file4.txt M % Save numbers with 16 decimal places.
type file4.txt % Display file contents in command window.


## 1.2. Low-Level ASCII Write

The primary function for writing an ASCII file is fprintf [1]. Other functions are available, but fprintf is the most general purpose ("low-level") function.

FPRINTF Write formatted data to file.
COUNT = FPRINTF(FID,FORMAT,A,...) formats the data in the real
part of array A (and in any additional array arguments), under
control of the specified FORMAT string, and writes it to the file
associated with file identifier FID.  COUNT is the number of bytes
successfully written. FID is an integer file identifier obtained
from FOPEN. It can also be 1 for standard output (the screen) or 2
for standard error. If FID is omitted, output goes to the screen.


### 1.2.1. Examples

In the following, several examples are given that are intended to give you an understanding of this somewhat terse explanation.

Example:

Write the number 10.00 to a file named mydata.txt.

% Create and open a file.
fileID = fopen('mydata.txt','w');
% Write the number 10.00 to then a new line.
fprintf(fileID,'%.2f\n',10.0);
% Close the file.
fclose(fileID);
% Display file contents in command window (or open using file browser)
type mydata.txt


If you type pwd on the command line, you should see the location where the file was written. You should be able to open this file with a text editor and see the value 10.00.

The %.2f\n is a format string. %.2f tells MATLAB how to format the number (number of decimal points, exponential notation, etc.). \n tells MATLAB to insert a newline into the file after writing the number.

The most common number formatting letters are f, d, E, e, and g.

Example:

Write the numbers 10.00 11.00 to a file named mydata.txt.

fileID = fopen('mydata.txt','w'); % Create and open a file.
fprintf(fileID,'%.2f %.2f\n',10.0,11.0); % Write the numbers 10.00 11.00 to the file and hit enter.
fclose(fileID); % Close the file.
type mydata.txt % Display file contents in command window (or open using file browser)


Experiments:

• Repeat the above, but replace 10.0 with 10. Does the file change?
• Repeat the above, but replace 10.0 with 10.0000. Does the file change?
• Write the numbers 10.00 11.00 12.0 13.0 to a file.
• You may inspect the size of the file after each write by using the command !ls -1. How does the file size depend on the number of values written to the file? Is it consistent with what you would expect if the file was encoded as 7- or 8-bit ASCII [2]?

Example:

Write the numbers 1.00, 2.00, ..., 10.00 to a file named mydata2.txt.

fileID = fopen('mydata2.txt','w'); % Create and open a file.
fprintf(fileID,'%.2f\n',1);
fprintf(fileID,'%.2f\n',2);
fprintf(fileID,'%.2f\n',3);
fprintf(fileID,'%.2f\n',4);
fprintf(fileID,'%.2f\n',5);
fprintf(fileID,'%.2f\n',6);
fprintf(fileID,'%.2f\n',7);
fprintf(fileID,'%.2f\n',8);
fprintf(fileID,'%.2f\n',9);
fprintf(fileID,'%.2f\n',10);
fclose(fileID); % Close the file.
type mydata2.txt


Experiments:

• Repeat the above, but delete all instances of \n.
• Repeat the above, but replace all instances of \n with three spaces.
• Re-write the above using a for loop.
• Repeat the above, but replace %.2f with %d.
• Repeat the above, but replace %.2f with %e.
• Repeat the above, but replace %.2f with %E.

### 1.2.2. Format Specifiers

In the above example, the sequence of characters %.2f is the format specifier. The \n is a special escape string that means "insert a newline". You don't see the newlines, but programs that read a file use them to determine where to start a new line.

The format specifier and escape string are short-hand notation that fprintf uses to determine how a number should be displayed.

The most commonly used escape sequences are

\n Newline ("carriage return")
\t Tab


In general, the format specifier has the following form (spaces included only for readability)

% Modifier FieldWidth . Precision FormatDescriptor


and not everything must be specified. In the example %.2f, no Modifier or FieldWidth was specified.

% Modifier FieldWidth . Precision FormatDescriptor
%     	       	       .    2  	       	 f


The possible values of Modifier are

- left-justify the number
+ include the sign of the number
0 pad the number with leading zeros (as many as needed to fill out FieldWidth)


FieldWidth is an integer that specifies how many characters the full number should span.

Precision is an integer that specifies how many values after the decimal point to show.

The most common FormatDescripters are

%d Display the number as an integer (e.g., 1, 99)
%f Display the number as floating point (e.g., 123., 123.00, 123.000)
%e Display in exponential format (e.g., 1.23e+05)
%E Display in exponential format (e.g., 1.23E+05)
%g Display only significant digits of the number


# 2. Problems

## 2.1. High-Level ASCII Write I

Create matrix and use save to create a file so that the following is displayed when type file.txt is entered on the command line.

  1.0000000e+00   6.0000000e+00
2.0000000e+00   7.0000000e+00
3.0000000e+00   8.0000000e+00
4.0000000e+00   9.0000000e+00
5.0000000e+00   1.0000000e+01


## 2.2. High-Level ASCII Write II

Create a matrix and use save to create a file so that the following is displayed when type file.txt is entered on the command line.

  1.0000000000000000e+00   6.0000000000000000e+00
2.0000000000000000e+00   7.0000000000000000e+00
3.0000000000000000e+00   8.0000000000000000e+00
4.0000000000000000e+00   9.0000000000000000e+00
5.0000000000000000e+00   1.0000000000000000e+01


## 2.3. High-Level ASCII Write III

Create an array and use save to create a file so that the following is displayed when type file.txt is entered on the command line.

  1.0000000000000000e+00   2.0000000000000000e+00   3.0000000000000000e+00   4.0000000000000000e+00


## 2.4. High-Level ASCII Write IV

Create an array and use save to create a file so that the following is displayed when type file.txt is entered on the command line.

  1.0000000e+00
2.0000000e+00
3.0000000e+00
4.0000000e+00


## 2.5. Format Specifiers - Format Descriptor

Attempt to discover how the g format descriptor works by creating a series of examples. After you think that you understand how it works, write a description of how it works in a few sentences. Compare your answer with the best description of the g identifier that you can find on the web.

## 2.6. Format Specifiers - Field Width

First, come up with several experiments that help you learn how field width specifiers work. For example, try

fprintf('%f',999);
fprintf('%10.2f',99);


Once you think that you understand the field width specifier, write a fprintf statement that produces each of the following outputs. In the following, blank spaces are indicated by ␣

␣␣␣␣10.1

␣␣␣␣+10.1

␣␣␣␣+10.1000

␣␣␣␣+1.01000E+01


## 2.7. Low-Level ASCII Write I

Write the numbers 10.000 and 11.000 to a file named file.txt using fprintf so that the command type file.txt displays

10.000 11.000


Write the numbers 10.000 and 11.000 to a file named file.txt using fprintf so that the command type file.txt displays (there are six spaces between the numbers and five spaces before the 10.000.

     10.000      11.000


## 2.8. Low-Level ASCII Write II

Write the numbers 10.000000 and 11.000000 to a file named file.txt using fprintf so that the command type file.txt displays

10.000000
11.000000


## 2.9. Low-Level ASCII Write III

Write a program, without using a for loop, that creates a file named file.txt so that type file.txt displays

1.00
2.00
3.00
4.00


## 2.10. Low-Level ASCII Write IV

Write a program using a for loop that creates a file named file.txt so that type file.txt displays

1.00
2.00
3.00
4.00


## 2.11. Low-Level ASCII Write V

Write a program, without using a for loop, that creates a file named file.txt so that type file.txt displays

1 2 3
4 5 6
7 8 9


## 2.12. Low-Level ASCII Write VI

Write a program, without using a for loop, that creates a file named file.txt so that type file.txt displays

1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0


## 2.13. Low-Level ASCII Write VII

Write a program, using a for loop, that creates a file named file.txt so that type file.txt displays

1 2 3
1 2 3
1 2 3


## 2.14. Low-Level ASCII Write VIII

The following program using

A = [1:9];

fid = fopen('HW10_6.txt','w');
for i = 1:length(A)
fprintf(fid,'%d ',A(i));
if mod(i,3) == 0
fprintf(fid,'\n');
end
end
fclose(fid);

type('HW10_6.txt')


creates a file with content

1 2 3
4 5 6
7 8 9


By editing only the first line and making an edit to only on other line, make the program create a file with content

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100


Save as HW10_6.m

## 2.15. Low-Level ASCII Write IX

The function documentation for fprintf notes that it is "vectorized":

FPRINTF is "vectorized" for the case when A is nonscalar. The
format string is recycled through the elements of A (columnwise)
until all the elements are used up. It is then recycled in a similar
manner through any additional array arguments.


As an example, the array

A = [10 11 12];


can be written using the non-vectorized form

clear;
A = [10 11 12];
fprintf('%d %d %d\n',A(1),A(2),A(3)); % Or equivalently fprintf('%d %d %d\n',10,11,12)
fclose(fid)


using

clear;
A = [10 11 12];
fprintf(fid,'%d %d %d\n',A);
fclose(fid);


In this case, there are three format strings and MATLAB sees that there is only one item in the list of values to use. It then checks if the first value is an array. If it is, it assumes that you meant to write the statement in the first form.

Now, suppose that there are three format strings but more than three values in the array:

clear;
A = [10 11 12 13 14 15];
fprintf(fid,'%d %d %d\n',A);
fclose(fid);


Or, that there are three format strings but more than three values in a matrix:

clear;
M = [10 11 12; 13 14 15; 16 17 18];
fprintf(fid,'%d %d %d\n',M);
fclose(fid);


If the numbers are not located where you want them, you may use transpose, flipud, and fliplr to modify the original matrix. For example,

clear;
M = [10 11 12; 13 14 15; 16 17 18];
Mmodified = transpose(M); % or flipud(M) or fliplr(M)
fprintf(fid,'%d %d %d\n',Mmodified);
fclose(fid);


### 2.15.1. Problem 1

Create a matrix or array of numbers in order of your choosing and then use fprintf in vectorized form to create the following file:

10 11 12
13 14 15
16 17 18


### 2.15.2. Problem 2

Starting with the matrix

M = [10 11 12; 13 14 15; 16 17 18];


use one or more of the functions transpose, flipud, and fliplr to modify the array so that the commands

fid = fopen('file.txt','w');
fprintf(fid,'%d %d %d\n',Mmodified);
fclose(fid);


will create the file

16 17 18
13 14 15
10 11 12


### 2.15.3. Problem 3

Starting with the matrix

M = [10 11 12; 13 14 15; 16 17 18];


use one or more of the functions transpose, flipud, and fliplr to modify the array so that the commands

fid = fopen('Username_vector_problem_2.txt','w');
fprintf(fid,'%d %d %d\n',Mmodified);
fclose(fid);


will create the file

16 13 10
17 14 11
18 15 12


## 2.16. Low-Level ASCII Write X

Write a program that generates a file named file1.txt with contents

None of your format specifiers may have a space in it. That is fprintf('  %f',10) is not allowed because of the two spaces that appear before the % sign.

10
10.000000
10.000000,10.000000
10.00

9.00

8.00


## 2.17. Low-level ASCII Write XI

Write a program that creates the following file (where the ... indicates lines 4-86399).

2000-01-01T00:00:00 0
2000-01-01T00:00:01 1
2000-01-01T00:00:02 2
...
2000-01-01T23:59:59 86399


## 2.18. Low-Level ASCII Write XI

Write a program that creates the following file (where the ... indicates lines 4-86399).

2000-01-01T00:00:00 0
2000-01-01T00:00:01 1
2000-01-01T00:00:02 2
...
2000-01-01T23:59:59 86399


## 2.19. Speed Comparison

In MATLAB, create an array of 2^16 random values using randn

Compare the file sizes and the time required to save this list to a file and then read the file:

• Using fwrite to save these values as doubles and fread to read the values back in.
• Using save -ascii -double to save these values and load to read the values back in.