[37] | 1 | function A = read(filename, format) |
---|
| 2 | % READ Input a quaternion array from a text file. The format |
---|
| 3 | % of the file is defined by the matching function WRITE (q.v.). |
---|
| 4 | % |
---|
| 5 | % A = read(filename, format) returns an array A of either full |
---|
| 6 | % or pure quaternions dependent on what was in the file. The |
---|
| 7 | % format parameter may be omitted, in which case a default is |
---|
| 8 | % supplied compatible with the function write. Otherwise, the |
---|
| 9 | % format parameter is as for the Matlab function scanf (q.v.). |
---|
| 10 | |
---|
| 11 | % Copyright © 2005 Stephen J. Sangwine and Nicolas Le Bihan. |
---|
| 12 | % See the file : Copyright.m for further details. |
---|
| 13 | |
---|
| 14 | error(nargchk(1, 2, nargin)), error(nargoutchk(0, 1, nargout)) |
---|
| 15 | |
---|
| 16 | if nargin == 1 |
---|
| 17 | |
---|
| 18 | % Only one parameter has been supplied. The missing one must |
---|
| 19 | % be the format, so supply the default value, to match the |
---|
| 20 | % default value used in the function write. Note that the |
---|
| 21 | % strings differ slightly because of the differences between |
---|
| 22 | % printf and scanf. |
---|
| 23 | |
---|
| 24 | format = '%24e'; |
---|
| 25 | end |
---|
| 26 | |
---|
| 27 | fid = fopen(filename, 'r'); |
---|
| 28 | |
---|
| 29 | r = fscanf(fid, ' %8u', 1); |
---|
| 30 | c = fscanf(fid, ' %8u', 1); |
---|
| 31 | n = r .* c; |
---|
| 32 | |
---|
| 33 | % Now read in the data into a real matrix, which we then re-arrange |
---|
| 34 | % to produce a quaternion matrix A. Note that we can deduce whether |
---|
| 35 | % the file contains pure or full quaternions from the number of |
---|
| 36 | % floating-point values read: it will be either 3 or 4 times the |
---|
| 37 | % number of quaternions indicated by n. |
---|
| 38 | |
---|
| 39 | [R, count] = fscanf(fid, [' ', format], inf); |
---|
| 40 | fclose(fid); |
---|
| 41 | |
---|
| 42 | inc = count ./ n; % This value is either 3 or 4, depending on whether |
---|
| 43 | % the file contained pure or full quaternions. |
---|
| 44 | |
---|
| 45 | if inc ~= 3 & inc ~= 4 |
---|
| 46 | error('The number of values read from the file is incorrect.'); |
---|
| 47 | end |
---|
| 48 | |
---|
| 49 | if inc == 4 |
---|
| 50 | A = quaternion(transpose(reshape(R(1 : inc : count - 3), c, r)), ... |
---|
| 51 | transpose(reshape(R(2 : inc : count - 2), c, r)), ... |
---|
| 52 | transpose(reshape(R(3 : inc : count - 1), c, r)), ... |
---|
| 53 | transpose(reshape(R(4 : inc : count ), c, r))); |
---|
| 54 | else |
---|
| 55 | A = quaternion(transpose(reshape(R(1 : inc : count - 2), c, r)), ... |
---|
| 56 | transpose(reshape(R(2 : inc : count - 1), c, r)), ... |
---|
| 57 | transpose(reshape(R(3 : inc : count ), c, r))); |
---|
| 58 | end |
---|