1 | function DataObj = subsasgn(DataObj, index, val) |
---|
2 | % SUBSASGN - Define index assignment for AccObj |
---|
3 | % |
---|
4 | % Written by Greg Portmann |
---|
5 | |
---|
6 | |
---|
7 | switch index(1).type |
---|
8 | case '{}' |
---|
9 | % Cell array of AccObj |
---|
10 | % This method will only get called if DataObj was previously defined |
---|
11 | % as an AccObj then one wants to redefine it as a cell array |
---|
12 | tmp = DataObj; |
---|
13 | DataObj = cell(index.subs{:}); |
---|
14 | DataObj{index.subs{:}} = tmp; |
---|
15 | |
---|
16 | case '()' |
---|
17 | if isempty(DataObj) |
---|
18 | % Start an array of AccObj |
---|
19 | clear DataObj |
---|
20 | % Build first rows |
---|
21 | tmp = AccObj; |
---|
22 | for i = 1:index.subs{1} |
---|
23 | DataObj(i,1) = tmp; |
---|
24 | end |
---|
25 | % Build the columns |
---|
26 | if length(index.subs) > 1 |
---|
27 | % Add new columns |
---|
28 | for cols = 1:index.subs{2}-1 |
---|
29 | DataObj = [DataObj DataObj(:,1)]; |
---|
30 | % Empty the new row |
---|
31 | tmp = AccObj; |
---|
32 | for i = 1:size(DataObj,1) |
---|
33 | DataObj(i,end) = tmp; |
---|
34 | end |
---|
35 | end |
---|
36 | end |
---|
37 | % Fill in the data |
---|
38 | DataObj(index.subs{:}) = val; |
---|
39 | |
---|
40 | elseif isa(DataObj, 'AccObj') & isa(val, 'AccObj') |
---|
41 | % If DataObj & val are both AccObj, then keep the array going |
---|
42 | if index.subs{1} > size(DataObj,1) & size(DataObj,2) > 1 |
---|
43 | % Add new rows |
---|
44 | for rows = 1:index.subs{1}-size(DataObj,1) |
---|
45 | DataObj = [DataObj;DataObj(1,:)]; |
---|
46 | % Empty the new row |
---|
47 | tmp = AccObj; |
---|
48 | for i = 1:size(DataObj,2) |
---|
49 | DataObj(end,i) = tmp; |
---|
50 | end |
---|
51 | end |
---|
52 | end |
---|
53 | if length(index.subs) > 1 |
---|
54 | if index.subs{2} > size(DataObj,2) & size(DataObj,1) > 1 |
---|
55 | % Add new columns |
---|
56 | for cols = 1:index.subs{2}-size(DataObj,2) |
---|
57 | DataObj = [DataObj DataObj(:,1)]; |
---|
58 | % Empty the new row |
---|
59 | tmp = AccObj; |
---|
60 | for i = 1:size(DataObj,1) |
---|
61 | DataObj(i,end) = tmp; |
---|
62 | end |
---|
63 | end |
---|
64 | end |
---|
65 | end |
---|
66 | DataObj(index.subs{:}) = val; |
---|
67 | |
---|
68 | else |
---|
69 | % True assignment change of the AccObj input |
---|
70 | Families = fieldnames(DataObj); |
---|
71 | Index1 = index.subs{1}; |
---|
72 | if length(index.subs) > 1 |
---|
73 | Index2 = index.subs{2}; |
---|
74 | else |
---|
75 | Index2 = ':'; |
---|
76 | end |
---|
77 | for i = 1:length(Families) |
---|
78 | if iscell(index.subs{1}) |
---|
79 | % If index1 is a cell then it's a device list |
---|
80 | Index1 = findrowindex(index.subs{1}{1}, DataObj.(Families{i}).DeviceList); |
---|
81 | if isempty(Index1) |
---|
82 | error('??? Device not found.'); |
---|
83 | end |
---|
84 | end |
---|
85 | if max(Index1) > size(DataObj.(Families{i}).Data,1) |
---|
86 | error(sprintf('Index is greater than the number of devices in family %s', Families{i})); |
---|
87 | end |
---|
88 | DataObj.(Families{i}).Data(Index1,Index2) = val; |
---|
89 | if isempty(val) |
---|
90 | % If val is empty then the request is to remove a device |
---|
91 | if isempty(DataObj.(Families{i}).Data) |
---|
92 | % Remove the family if there is no data left |
---|
93 | DataObj.(Families{i}) = []; |
---|
94 | else |
---|
95 | % Remove part of the family |
---|
96 | DataObj.(Families{i}).DeviceList(Index1,:) = val; |
---|
97 | DataObj.(Families{i}).Status(Index1,:) = val; |
---|
98 | end |
---|
99 | end |
---|
100 | end |
---|
101 | end |
---|
102 | |
---|
103 | case '.' |
---|
104 | % AccObj.(Family) input |
---|
105 | % Assigment change for only one family |
---|
106 | Family = index(1).subs; |
---|
107 | if length(index) > 1 |
---|
108 | Index1 = index(2).subs{1}; |
---|
109 | if length(index(2).subs) > 1 |
---|
110 | Index2 = index(2).subs{2}; |
---|
111 | else |
---|
112 | Index2 = ':'; |
---|
113 | end |
---|
114 | else |
---|
115 | DataObj.(Family) = []; |
---|
116 | return; |
---|
117 | end |
---|
118 | if iscell(index(2).subs{1}) |
---|
119 | % If index1 is a cell then it's a device list |
---|
120 | Index1 = findrowindex(index(2).subs{1}{1}, DataObj.(Family).DeviceList); |
---|
121 | if isempty(Index1) |
---|
122 | error('??? Device not found.'); |
---|
123 | end |
---|
124 | end |
---|
125 | if max(Index1) > size(DataObj.(Family).Data,1) |
---|
126 | error('Index is greater than the number of devices'); |
---|
127 | end |
---|
128 | DataObj.(Family).Data(Index1,Index2) = val; |
---|
129 | if isempty(val) |
---|
130 | if isempty(DataObj.(Family).Data) |
---|
131 | % Remove the family if there is no data left |
---|
132 | DataObj.(Family) = []; |
---|
133 | else |
---|
134 | % If val is empty then the request is to remove a device |
---|
135 | DataObj.(Family).DeviceList(Index1,:) = val; |
---|
136 | DataObj.(Family).Status(Index1,:) = val; |
---|
137 | end |
---|
138 | end |
---|
139 | end |
---|
140 | return |
---|
141 | |
---|