-
Notifications
You must be signed in to change notification settings - Fork 1
/
assignments2StructOrObj.m
82 lines (71 loc) · 3.82 KB
/
assignments2StructOrObj.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
function s = assignments2StructOrObj(str,s)
%ASSIGNMENTS2STRUCTOROBJ Create a struct, or configure a handle object,
%from a string generated by structOrObj2Assignments.
% s = assignments2Struct(str,s)
%
% str: string generated by structOrObj2Assignments
% s [input]: (optional, scalar handle object). Object to configure.
% s [output]: If a handle object is supplied as s, that object is returned
% in s. If no object is supplied, a structure is created and returned in s.
% acquired from scanimage4/most.util.assignments2StructOrObj
%
% The Janelia Farm Research Center Software Copyright 1.0
%
% Copyright (c) 2006, 2007, Howard Hughes Medical Institute, All rights reserved.
%
% Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
%
% Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
% Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
% Neither the name of the Howard Hughes Medical Institute nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT, OR FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; REASONABLE ROYALTIES; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
if nargin < 2
s = struct();
end
rows = textscan(str,'%s','Delimiter','\n');
rows = rows{1};
if isempty(rows)
return;
end
for c = 1:numel(rows)
row = rows{c};
% replace top-level name with 'obj'
[~, rmn] = strtok(row,'.');
row = ['s' rmn];
% deal with nonscalar nested structs/objs
pat = '([\w]+)__([0123456789]+)\.';
replc = '$1($2).';
row = regexprep(row,pat,replc);
% handle unencodeable value or nonscalar struct/obj.
% Note: structOrObj2Assignments, assignments2StructOrObj, and toString
% (all in most.util) are in cahoots with respect
% to these hardcoded strings.
unencodeval = '<unencodeable value>';
if strfind(row,unencodeval)
row = strrep(row,unencodeval,'[]');
end
nonscalarstructobjstr = '<nonscalar struct/object>';
if strfind(row,nonscalarstructobjstr)
row = strrep(row,nonscalarstructobjstr,'[]');
end
% handle ND array format produced by array2Str
try
if ~isempty(strfind(row,'&'))
equalsIdx = strfind(row,'=');
[dimArr rmn] = strtok(row(equalsIdx+1:end),'&');
arr = strtok(rmn,'&');
arr = reshape(str2num(arr),str2num(dimArr)); %#ok<NASGU,ST2NM>
eval([row(1:equalsIdx+1) 'arr;']);
else
eval([row ';']);
end
catch ME %Warn if assignments to no-longer-extant properties are found
if strcmpi(ME.identifier,'MATLAB:noPublicFieldForClass')
equalsIdx = strfind(row,'=');
fprintf(1,'WARNING: Property ''%s'' was specified, but does not exist for class ''%s''\n', deblank(row(3:equalsIdx-1)),class(s));
else
ME.rethrow();
end
end
end
end