Illustration of Writing to files:
Here is the other illustration in which a matrix is written to a file. At First, an arbitrary 2 × 4 matrix is generated, and then it is written to the file by using the format string %d %d\n', that means that each column from the matrix will be written as an individual line in the file.
>> mat = randint(2,4,[5 20])
mat =
20 14 19 12
8 12 17 5
>> fid = fopen('randmat.dat','w');
>> fprintf(fid,'%d %d\n',mat);
>> fclose(fid);
As this is a matrix, the load function is used to read it in.
>> load randmat.dat
>> randmat
randmat =
20 8
14 12
19 17
12 5
>> randmat'
ans =
20 14 19 12
8 12 17 5
Transposing the matrix will show in the form of the original matrix. When you want this to start with, the matrix variable mat can be transposed before using the fprintf to write to the file. (Obviously, it would be much easier in this situation to just use save rather than!)