-
Notifications
You must be signed in to change notification settings - Fork 0
/
structobj.m
514 lines (454 loc) · 17.5 KB
/
structobj.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
classdef structobj < handle
% structobj - Mimics a structure but can be passed by reference
%
% This class was developed to completely wrap the behavior of struct
% in a class. It has the added benefit that it can be passed around
% as a handle (by reference) so if a function or the user modifies
% the information contained within, all other copies of the
% information will see the change (and can react to it using the
% Updated event).
%
% The resulting handle to the structobj object can be modified and
% used just like a structure would ordinarily be used.
%
% USAGE:
% self = structobj(S)
%
% INPUTS:
% S: struct, Structure containing all of the data to be stored.
%
% OUTPUTS:
% self: Handle, Handle to the structobj object which can be used
% just like a struct to modify the contents.
% Copyright (c) <2016> Jonathan Suever ([email protected]
% All rights reserved
%
% This software is licensed using the 3-clause BSD license.
properties (Access = 'private')
Data = repmat(struct(), [0 1]); % Structure holding all the data
end
events
Updated % Event that is fired any time the data is modified
end
methods
function self = structobj(varargin)
% structobj - Constructor for the structobj object
%
% USAGE:
% self = structobj(S)
% self = structobj(varargin);
%
% INPUTS:
% S: struct, Structure containing all of the data to
% be stored.
%
% varargin: Mixed, Inputs typically passed to the struct
% constructor.
%
% OUTPUTS:
% self: Handle, Handle to the structobj object which can
% be used just like a struct to modify the contents.
if nargin == 0
S = struct();
elseif ~isstruct(varargin{1})
S = struct(varargin{:});
else
S = varargin{1};
end
% If a multi-dimensional struct is used, then create a
% multidimensional structobj object
if numel(S) == 1
self.Data = S(1);
else
self = structobj.empty();
for k = 1:numel(S)
self = cat(1, self, structobj(S(k)));
end
self = reshape(self, size(S));
end
end
function res = copy(self)
% copy - Makes a complete (shallow) copy of the object
%
% This makes a shallow copy in that if you have any handle
% object instances saved in the object, these are copied by
% reference. The main structobj object, however, is copied
% and the result is unlinked with the original instance.
%
% USAGE;
% obj = self.copy()
%
% OUTPUTS:
% obj: Handle, Handle to a duplicate structobj object
res = structobj.loadobj(self.saveobj());
end
function disp(self)
% disp - Overloaded display function
%
% The DISP method was overloaded to display the underlying
% data just like it would be if it were a structure. This is
% further improve the seamless usage of structobj as
% opposed to a standard struct.
%
% USAGE:
% self.disp()
disp(struct(self));
end
function self = horzcat(self, varargin)
% horzcat - Concatenate objects in the 2nd dimension
%
% This overloaded function first ensures that all members
% contain the same fields and can be concatenated (similar to
% the behavior for structs).
%
% USAGE:
% obj = self.horzcat(obj2)
%
% INPUTS:
% obj2: Handle, structobj object to be concatenated with
% the current object.
%
% OUTPUTS:
% obj: Handle, Array of the concatenation of the two
% structobj objects.
self.checkfields(varargin{:})
self = builtin('horzcat', self, varargin{:});
end
function props = properties(self)
% properties - Get stored properties and fields
%
% This returns a list of all the properties that are stored
% within the object. In older versions of MATLAB (< 2014a),
% overloading this function would allow for tab completion.
%
% USAGE:
% names = self.fieldnames()
%
% OUTPUTS:
% names: Cell Array, Cell array of strings where each
% element corresponds to one of the fields stored in
% the data structure
props = fieldnames(self);
end
function out = saveobj(self)
% saveobj - Saves the necessary data to create an object
%
% USAGE:
% res = self.saveobj()
%
% OUTPUTS;
% res: Struct, Structure containing the properties
% necessary to re-create an identical object.
out = struct(self);
end
function self = subsasgn(self, s, varargin)
% subsasgn - Overloaded subscript assignment function
%
% This method was overloaded to allow the user to add/modify
% properties of the object just like they would with a
% structure (dot notation, etc.). This results in nearly
% identical behavior except for some limitations thanks to
% MATLAB bugs.
if ~isempty(s) && isequal(s(1).type, '()')
obj = builtin('subsref', self, s(1));
s = s(2:end);
else
obj = self;
end
dat = [obj.Data];
dat = reshape(dat, size(obj));
if isempty(s)
self = obj;
return
end
try
out = builtin('subsasgn', dat, s, varargin{:});
catch ME
valid = {'MATLAB:noPublicFieldForClass'
'MATLAB:noSuchMethodOrField'};
if any(strcmpi(ME.identifier, valid))
item = subsref(dat, s(1));
ignore = subsasgn(item, s(2:end), varargin{:}); %#ok
out = dat;
else
rethrow(ME);
end
end
out = num2cell(out);
[obj.Data] = deal(out{:});
end
function varargout = subsref(self, s)
% subsref - Overloaded subscript referencing function
%
% This method was overloaded to allow the user to view the
% properties of the object just like they would with a
% structure (dot notation, etc.). This results in nearly
% identical behavior except for some limitations thanks to
% MATLAB bugs.
if numel(s) == 1 && isequal(s(1).type, '()')
obj = builtin('subsref', self, s(1));
varargout = {obj};
return
end
[varargout{1:nargout}] = builtin('subsref', struct(self), s(1));
if numel(s) > 1
if isa(varargout, class(self))
[varargout{:}] = varargout{1}.subsref(s(2:end));
else
[varargout{:}] = subsref(varargout{:}, s(2:end));
end
end
end
function update(self, obj)
% update - Updates the current object with data from a struct
%
% This function allows the user to quickly merge an existing
% structure with the current object.
%
% USAGE:
% self.update(S)
%
% INPUTS:
% S: struct, Structure or structure-like object whose values
% should be incorporated into the current object
% Test to see if the alternate object is easily converted
if ~isequal(size(self), size(obj))
error(sprintf('%s:MismatchSize', mfilename), ...
'Sizes must match');
end
if numel(self) > 1
arrayfun(@update, self, obj);
return;
end
try
obj = struct(obj);
catch
error(sprintf('%s:InvalidStruct', mfilename), ...
'Requires a structure-like input');
end
fields = fieldnames(obj);
for k = 1:numel(fields)
[self.Data.(fields{k})] = deal(obj.(fields{k}));
end
end
function self = vertcat(self, varargin)
% vertcat - Concatenate objects in the 1st dimension
%
% This overloaded function first ensures that all members
% contain the same fields and can be concatenated (similar to
% the behavior for structs).
%
% USAGE:
% obj = self.vertcat(obj2)
%
% INPUTS:
% obj2: Handle, structobj object to be concatenated with
% the current object.
%
% OUTPUTS:
% obj: Handle, Array of the concatenation of the two
% structobj objects.
self.checkfields(varargin{:})
self = builtin('vertcat', self, varargin{:});
end
end
%--- Overloaded struct functions ---%
methods
function res = struct(self)
% struct - Converts structobj to a structure array
%
% Converts the structobj back into a struct which can no
% longer be passed around by reference.
%
% USAGE:
% S = self.struct()
%
% OUTPUTS;
% S: Struct, Structure containing all of the same data as
% the structobj object except it can no longer be
% passed around as a handle.
if isempty(self)
res = repmat(struct(), size(self));
else
res = reshape([self.Data], size(self));
end
end
function res = fieldnames(self)
% fieldnames - Get stored properties and fields
%
% This returns a list of all the properties that are stored
% within the object. In older versions of MATLAB (< 2014a),
% overloading this function would allow for tab completion.
%
% USAGE:
% names = self.fieldnames()
%
% OUTPUTS:
% names: Cell Array, Cell array of strings where each
% element corresponds to one of the fields stored in
% the data structure
res = fieldnames(struct(self));
end
function res = getfield(self, field, default)
% getfield - Get the value of a specific field
%
% Overloaded version of the getfield method for structures.
% This implementation varies in one way in that it can accept
% an additional input which specifies a default value to
% return if the requested field does not exist.
%
% USAGE:
% val = self.getfield(fieldname, default)
%
% INPUTS:
% fieldname: String, Name of the property to return
%
% default: The default value to return if the requested
% property does not exist.
%
% OUTPUTS:
% val: The value of the requested field
if ~isfield(self, field) && exist('default', 'var')
if numel(self) > 1
res = repmat({default}, size(self));
else
res = default;
end
else
res = getfield(struct(self), field);
end
end
function res = isfield(self, varargin)
% isfield - Determines whether the object has a specific field
%
% USAGE:
% bool = self.isfield(fieldname)
%
% INPUTS:
% fieldname: String, Name of the property to return
%
% OUTPUTS:
% bool: Logical, TRUE if the field exists and FALSE
% otherwise
res = isfield(struct(self), varargin{:});
end
function res = isstruct(varargin)
% isstruct - Overloaded function to trick built-in functions
%
% Many built-in functions expect a structure as an input and
% this method allows structobj objects to be passed in
% lieu of structures by falsely reporting that they are
% structures. This works because we have also overloaded all
% struct functionality.
%
% USAGE:
% bool = self.isstruct()
%
% OUTPUTS:
% bool: Logical, This value is ALWAYS TRUE
res = true;
end
function [self, perm] = orderfields(self, varargin)
% orderfields - Order the fields of the object
%
% The typical usage of this functions is to enforce a
% specific ordering of the object properties. See the help
% for the built-in ORDERFIELDS function for more information.
%
% USAGE:
% [self, perm] = self.orderfields()
%
% OUTPUTS:
% self: Handle, structobj object (with the same handle)
% with the fields ordered as requested.
%
% perm: Vector, A permutation vector representing the
% change in order performed on the fields of the
% object that resulted in the output object.
for k = 1:numel(self)
[self(k).Data, perm] = orderfields(self(k).Data, varargin{:});
end
end
function self = rmfield(self, varargin)
% rmfield - Removes the specified fields from the object
%
% USAGE:
% self = self.rmfield(fields)
%
% INPUTS:
% fields: String or Cell Array, Names of the fields to remove
%
% OUTPUTS:
% self: Handle, structobj object with the specified fields
% removed.
for k = 1:numel(self)
self(k).Data = rmfield(self(k).Data, varargin{:});
end
end
end
%--- Get / Set Functions ---%
methods
function set.Data(self, val)
self.Data = val;
notify(self, 'Updated');
end
end
methods (Access = 'private')
function checkfields(varargin)
% checkfields - Ensures that all members have the same fields
%
% In order to behave like a structure, it is necessary that
% all members of a multi-dimensional structobj object have
% the same properties.
%
% An error is thrown if the fields do not match for all input
% objects.
%
% USAGE:
% self.checkfields()
fields = cellfun(@(x)sort(fieldnames(x)), varargin, 'uni', 0);
equal = cellfun(@(x)isequal(x, fields{1}), fields(2:end));
if ~all(equal)
error(sprintf('%s:MismatchedFields', mfilename), ...
'Unmatched structure fields. Cannot concatenate');
end
end
end
methods (Static)
function res = loadobj(S)
% loadobj - Instantiate an object from a structure
%
% This function is called when loading an object from a .mat
% file. Alternately, this can be used if you just want to
% quickly construct an object from a structure.
%
% USAGE:
% self = loadobj(S)
%
% INPUTS:
% S: Struct, Structure containing all the information
% required to properly construct the object
%
% OUTPUTS:
% self: Object, Instance of structobj that is the same
% dimensionality as the input structure.
res = structobj(S);
end
function results = test(varargin)
% structobj.test - Runs all unittests
%
% USAGE:
% results = structobj.test(name);
%
% INPUTS:
% name: String, Name of a specific test to run (optional)
%
% OUTPUTS:
% results: TestResult, matlab.unittest.TestResult object
% that provides detailed information about which
% tests passed or failed.
tests = test_structobj();
results = tests.run(varargin{:});
end
end
end