1 | function b = subsref(a, ss) |
---|
2 | % SUBSREF Subscripted reference. |
---|
3 | % (Quaternion overloading of standard Matlab function.) |
---|
4 | |
---|
5 | % Copyright © 2005 Stephen J. Sangwine and Nicolas Le Bihan. |
---|
6 | % See the file : Copyright.m for further details. |
---|
7 | |
---|
8 | if length(ss) ~= 1 |
---|
9 | error('Only one level of subscripting is currently supported for quaternions.'); |
---|
10 | % See the notes below under structure indexing. |
---|
11 | end |
---|
12 | |
---|
13 | switch ss.type |
---|
14 | case '()' |
---|
15 | if length(ss) ~= 1 |
---|
16 | error('Multiple levels of subscripting are not supported for quaternions.') |
---|
17 | end |
---|
18 | |
---|
19 | % To implement indexing, we separate the quaternion into components, and then |
---|
20 | % compose the quaternion from the indexed components. |
---|
21 | |
---|
22 | if ispure(a) |
---|
23 | xa = x(a); ya = y(a); za = z(a); |
---|
24 | b = quaternion(xa(ss.subs{:}), ya(ss.subs{:}), za(ss.subs{:})); |
---|
25 | else |
---|
26 | sa = s(a); xa = x(a); ya = y(a); za = z(a); |
---|
27 | b = quaternion(sa(ss.subs{:}), xa(ss.subs{:}), ya(ss.subs{:}), za(ss.subs{:})); |
---|
28 | end |
---|
29 | case '{}' |
---|
30 | error('Cell array indexing is not valid for quaternions.') |
---|
31 | case '.' |
---|
32 | % Structure indexing. |
---|
33 | % |
---|
34 | % See some notes on this subject in the file subsasgn.m. Here, we would |
---|
35 | % have to support two levels of indexing, such as q.x(1,2) and q(1,2).x |
---|
36 | % which would give the same result. The Matlab help on subsref explains |
---|
37 | % how this would work. We could accept s, x, y, or z as field names and |
---|
38 | % return x(a) etc, but we would really need to pass a second level of |
---|
39 | % subscripting back to subsref recursively. The code below is an |
---|
40 | % interim step to support one level of structure indexing. |
---|
41 | % |
---|
42 | % 15-Sep-2005 Code contributed by T.A. Ell. |
---|
43 | % |
---|
44 | switch ss.subs |
---|
45 | case {'vector', 'v'} |
---|
46 | b = vector(a); % maybe private function access would be better? |
---|
47 | case {'scalar', 's'} |
---|
48 | b = scalar(a); |
---|
49 | case {'x', 'I'} |
---|
50 | b = x(a); |
---|
51 | case {'y', 'J'} |
---|
52 | b = y(a); |
---|
53 | case {'z', 'K'} |
---|
54 | b = z(a); |
---|
55 | case 'imag' |
---|
56 | b = imag(a); |
---|
57 | case {'real'} |
---|
58 | b = real(a); |
---|
59 | otherwise |
---|
60 | error( ['Structure ''.', ss.subs, ''' is not a valid index']); |
---|
61 | end |
---|
62 | otherwise |
---|
63 | error('subsref received an invalid subscripting type.') |
---|
64 | end |
---|