a b/functions/@memmapdata/subsref.m
1
% SUBSREF - index eegdata class
2
%
3
% Author: Arnaud Delorme, SCCN, INC, UCSD, Nov. 2008
4
5
% Copyright (C) 2008 Arnaud Delorme, SCCN, INC, UCSD
6
%
7
% This file is part of EEGLAB, see http://www.eeglab.org
8
% for the documentation and details.
9
%
10
% Redistribution and use in source and binary forms, with or without
11
% modification, are permitted provided that the following conditions are met:
12
%
13
% 1. Redistributions of source code must retain the above copyright notice,
14
% this list of conditions and the following disclaimer.
15
%
16
% 2. Redistributions in binary form must reproduce the above copyright notice,
17
% this list of conditions and the following disclaimer in the documentation
18
% and/or other materials provided with the distribution.
19
%
20
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30
% THE POSSIBILITY OF SUCH DAMAGE.
31
32
function b = subsref(a,s)
33
    
34
    if s(1).type == '.'
35
        b = builtin('subsref', struct(a), s); return;
36
    end
37
        
38
    subs = s(1).subs;
39
    finaldim = cellfun('length', subs);
40
    
41
    % one dimension input
42
    % -------------------
43
    if length(s(1).subs) == 1 
44
        if ischar(subs{1})
45
            subs{1} = [1:size(a,1)];
46
            subs{2} = [1:size(a,2)];
47
            if ndims(a) == 3, subs{3} = [1:size(a,3)]; end
48
            finaldim = prod(size(a));
49
        end
50
        
51
    % two dimension input
52
    % -------------------
53
    elseif length(s(1).subs) == 2 
54
        if ischar(subs{1}), subs{1} = [1:size(a,1)]; end
55
        
56
        if ischar(subs{2}),
57
            subs{2} = [1:size(a,2)];
58
            if ndims(a) == 3, subs{3} = [1:size(a,3)]; end
59
        end
60
        if length(subs) == 3
61
             finaldim = [ length(subs{1}) length(subs{2})*length(subs{3}) ];
62
        else finaldim = [ length(subs{1}) length(subs{2}) ];
63
        end
64
            
65
    % three dimension input
66
    % ---------------------
67
    elseif length(s(1).subs) == 3
68
        
69
        if ischar(subs{1}), subs{1} = [1:size(a,1)]; end
70
        if ischar(subs{2}), subs{2} = [1:size(a,2)]; end
71
        if ndims(a) == 2, 
72
            subs(3) = []; 
73
        else
74
            if ischar(subs{3}), subs{3} = [1:size(a,3)]; end
75
        end
76
        finaldim = cellfun('length', subs);
77
     
78
    end
79
80
    % non-transposed data
81
    % -------------------
82
    if ~strcmpi(a.fileformat, 'transposed')
83
        if length(subs) == 1, b = a.data.data.x(subs{1}); end
84
        if length(subs) == 2, b = a.data.data.x(subs{1}, subs{2}); end
85
        if length(subs) == 3, b = a.data.data.x(subs{1}, subs{2}, subs{3}); end
86
    else
87
        if ndims(a) == 2
88
            %if length(s) ==         0, b = transpose(a.data.data.x); return; end
89
            if length(s(1).subs) == 1, b = a.data.data.x(s(1).subs{1})'; end
90
            if length(s(1).subs) == 2, b = a.data.data.x(s(1).subs{2}, s(1).subs{1})'; end
91
            if length(s(1).subs) == 3, b = a.data.data.x(s(1).subs{2}, s(1).subs{1})'; end
92
        else
93
            %if length(s) ==         0, b = permute(a.data.data.x, [3 1 2]); return; end
94
            if length(subs) == 1,
95
                inds1 = mod(subs{1}-1, size(a,1))+1;
96
                inds2 = mod((subs{1}-inds1)/size(a,1), size(a,2))+1;
97
                inds3 = ((subs{1}-inds1)/size(a,1)-inds2+1)/size(a,2)+1;
98
                inds  = (inds1-1)*size(a,2)*size(a,3) + (inds3-1)*size(a,2) + inds2;
99
                b = a.data.data.x(inds);
100
            else
101
                if length(subs) < 2, subs{3} = 1; end
102
                
103
                % repmat if several indices in different dimensions
104
                % -------------------------------------------------
105
                len = cellfun('length', subs);
106
                subs{1} = repmat(reshape(subs{1}, [len(1) 1 1]), [1 len(2) len(3)]);
107
                subs{2} = repmat(reshape(subs{2}, [1 len(2) 1]), [len(1) 1 len(3)]);
108
                subs{3} = repmat(reshape(subs{3}, [1 1 len(3)]), [len(1) len(2) 1]);
109
                
110
                inds = (subs{1}-1)*a.data.Format{2}(1)*a.data.Format{2}(2) + (subs{3}-1)*a.data.Format{2}(1) + subs{2};
111
                inds = reshape(inds, [1 prod(size(inds))]);
112
                b = a.data.data.x(inds);
113
            end
114
        end
115
    end
116
 
117
    if length(finaldim) == 1, finaldim(2) = 1; end
118
    b = reshape(b, finaldim);
119
120
% 2 dims
121
%inds1 = mod(myinds-1, size(a,1))+1;
122
%inds2 = (myinds-inds1)/size(a,1)+1;
123
%inds  = (inds2-1)*size(a,1) + inds1;
124
125
% 3 dims
126
%inds1 = mod(myinds-1, size(a,1))+1;
127
%inds2 = mod((myinds-inds1)/size(a,1), size(a,2))+1;
128
%inds3 = ((myinds-inds1)/size(a,1)-inds2)/size(a,2)+1;
129
%inds  = (inds3-1)*size(a,1)*size(a,2) + inds2*size(a,1) + inds1;
130
131