1 | function write(filename, format, a) |
---|
2 | % WRITE Output a quaternion array to a text file. |
---|
3 | % |
---|
4 | % write(filename, format, a) |
---|
5 | % |
---|
6 | % This function writes a text file which is designed to be easily |
---|
7 | % read into other software, such as Maple, Mathematica, etc. The |
---|
8 | % first line of the file contains two integer values giving the |
---|
9 | % number of rows and columns in the file. The quaternion values |
---|
10 | % then follow in raster order, that is with the column index varying |
---|
11 | % most rapidly. Each quaternion value occupies one line in the file, |
---|
12 | % and consists of four floating-point values, separated by spaces. |
---|
13 | % |
---|
14 | % The format parameter may be omitted, in which case a default is |
---|
15 | % assumed which outputs sufficient digits to represent double values |
---|
16 | % with no loss of accuracy. Otherwise the format is a string (see the |
---|
17 | % standard Matlab function fprintf for details). |
---|
18 | % |
---|
19 | % If the parameter a is a pure quaternion, only three components are |
---|
20 | % written per quaternion. |
---|
21 | |
---|
22 | % Copyright © 2005 Stephen J. Sangwine and Nicolas Le Bihan. |
---|
23 | % See the file : Copyright.m for further details. |
---|
24 | |
---|
25 | error(nargchk(2, 3, nargin)), error(nargoutchk(0, 0, nargout)) |
---|
26 | |
---|
27 | if nargin == 2 |
---|
28 | |
---|
29 | % Only two parameters have been supplied. The missing one must |
---|
30 | % be the format, since the other two cannot be omitted. Shuffle |
---|
31 | % the parameters, and supply the default value for the format. |
---|
32 | |
---|
33 | a = format; |
---|
34 | format = '%+24.16e'; |
---|
35 | end |
---|
36 | |
---|
37 | [r, c] = size(a); |
---|
38 | |
---|
39 | fid = fopen(filename, 'w'); |
---|
40 | |
---|
41 | fprintf(fid, '%8u %8u\n', r, c); |
---|
42 | fprintf(fid, format, a); |
---|
43 | fprintf(fid, '\n'); % For some reason the last \n gets omitted in the previous line. |
---|
44 | |
---|
45 | if fclose(fid) |
---|
46 | error('The file was not written successfully.'); |
---|
47 | end |
---|