Mercurial > hg > ltpda
comparison m-toolbox/classes/@plist/string.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 % STRING converts a plist object to a command string which will recreate the plist object. | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % DESCRIPTION: STRING converts a plist object to a command string which will | |
5 % recreate the plist object. | |
6 % | |
7 % CALL: cmd = string(pl) | |
8 % | |
9 % <a href="matlab:utils.helper.displayMethodInfo('plist', 'string')">Parameters Description</a> | |
10 % | |
11 % VERSION: $Id: string.m,v 1.45 2011/04/08 08:56:21 hewitson Exp $ | |
12 % | |
13 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
14 | |
15 function varargout = string(varargin) | |
16 | |
17 %%% Check if this is a call for parameters | |
18 if utils.helper.isinfocall(varargin{:}) | |
19 varargout{1} = getInfo(varargin{3}); | |
20 return | |
21 end | |
22 | |
23 objs = utils.helper.collect_objects(varargin(:), 'plist'); | |
24 | |
25 pstr = '['; | |
26 | |
27 for ll = 1:numel(objs) | |
28 pl = objs(ll); | |
29 pstr = sprintf('%splist(', pstr); | |
30 for j=1:pl.nparams | |
31 p = pl.params(j); | |
32 pVal = p.getVal; | |
33 if ischar(pVal) | |
34 %---- CHAR | |
35 val = sprintf('''%s''', strrep(pVal, '''', '''''')); | |
36 | |
37 elseif isnumeric(pVal) | |
38 %---- NUMERIC | |
39 val = sprintf('[%s]', utils.helper.mat2str(pVal)); | |
40 | |
41 elseif islogical(pVal) | |
42 %---- LOGICAL | |
43 val = sprintf('[%s]', mat2str(pVal)); | |
44 | |
45 elseif isjava(pVal) | |
46 %---- JAVA | |
47 if strcmp(class(pVal), 'sun.util.calendar.ZoneInfo') | |
48 val = sprintf('java.util.TimeZone.getTimeZone(''%s'')',char(pVal.getID)); | |
49 else | |
50 error('### Unknown java object [%s]', class(pVal)); | |
51 end | |
52 | |
53 elseif iscell(pVal) | |
54 %---- CELL | |
55 val = sprintf('%s', utils.prog.mcell2str(pVal)); | |
56 | |
57 elseif isstruct(pVal) | |
58 %---- STRUCT | |
59 ss = pVal; | |
60 ss_str = '['; | |
61 fields = fieldnames(ss); | |
62 for oo = 1:numel(pVal) | |
63 ss_str = sprintf('%s struct(', ss_str); | |
64 for ii = 1:numel(fields) | |
65 if isnumeric(ss(oo).(fields{ii})) || islogical(ss(oo).(fields{ii})) | |
66 ss_str = sprintf('%s''%s'', [%s], ', ss_str, fields{ii}, mat2str(ss(oo).(fields{ii}), 17)); | |
67 elseif ischar(ss(oo).(fields{ii})) | |
68 ss_str = sprintf('%s''%s'', ''%s'', ',ss_str, fields{ii}, strrep(ss(oo).(fields{ii}), '''', '''''')); | |
69 elseif isa(ss(oo).(fields{ii}), 'ltpda_obj') | |
70 ss_str = sprintf('%s''%s'', %s, ', ss_str, fields{ii}, string(ss(oo).(fields{ii}))); | |
71 elseif isa(ss(oo).(fields{ii}), 'sym') | |
72 symstr = char(ss(oo).(fields{ii})); | |
73 ss_str = sprintf('%s''%s'', sym(''%s''), ', ss_str, fields{ii}, symstr); | |
74 elseif isjava(ss(oo).(fields{ii})) | |
75 if strcmp(class(ss(oo).(fields{ii})), 'sun.util.calendar.ZoneInfo') | |
76 ss_str = sprintf('%s''%s'', java.util.TimeZone.getTimeZone(''%s''), ', ss_str, fields{ii}, char(getID(ss(oo).(fields{ii})))); | |
77 else | |
78 error('### Unknown java object [%s]', class(ss(oo).(fields{ll}))); | |
79 end | |
80 else | |
81 error('### Unknown type [%s] in struct', class(ss(oo).(fields{ii}))); | |
82 end | |
83 end | |
84 ss_str = [ss_str(1:end-2), ')']; | |
85 end | |
86 val = sprintf('%s]', ss_str); | |
87 | |
88 elseif isa(pVal, 'sym') | |
89 %---- SYM | |
90 val = sprintf('sym(''%s'')', char(pVal)); | |
91 | |
92 elseif isa(pVal, 'history') | |
93 %---- HISTORY | |
94 if ~isa(pVal.inhists, 'history') | |
95 cmds = hist2m(pVal); | |
96 [s,r] = strtok(cmds{2}, '='); | |
97 val = regexprep(strtrim(r(2:end)), ';[ ]*%.*', ''); | |
98 else | |
99 error('### Can not run string on an object containing history. Use type() instead.'); | |
100 end | |
101 | |
102 elseif isa(pVal, 'ltpda_nuo') || isa(pVal, 'plist') | |
103 %---- Non-user object or plist | |
104 val = string(pVal); | |
105 | |
106 elseif isa(pVal, 'ltpda_uoh') | |
107 %---- Object with history | |
108 if isempty(pVal.hist) | |
109 val = string(pVal); | |
110 else | |
111 if ~isa(pVal.hist.inhists, 'history') | |
112 cmds = hist2m(pVal.hist); | |
113 [s,r] = strtok(cmds{2}, '='); | |
114 val = regexprep(strtrim(r(2:end)), ';[ ]*%.*', ''); | |
115 else | |
116 error('### Can not run string on an object containing history. Use type() instead.'); | |
117 end | |
118 end | |
119 else | |
120 error('!!! Can not handle the class [%s].', class(pVal)); | |
121 end | |
122 | |
123 pstr = sprintf('%s''%s'', %s, ', pstr, p.key, val); | |
124 | |
125 end | |
126 | |
127 pstr = strtrim(pstr); | |
128 if pstr(end)==',' | |
129 pstr = pstr(1:end-1); | |
130 end | |
131 | |
132 % close bracket | |
133 pstr = [pstr '), ']; | |
134 end | |
135 | |
136 % Finish string | |
137 pstr = strtrim(pstr); | |
138 pstr = pstr(1:end-1); | |
139 pstr = [pstr ']']; | |
140 | |
141 varargout{1} = pstr; | |
142 end | |
143 | |
144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
145 % Local Functions % | |
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
147 | |
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
149 % | |
150 % FUNCTION: getInfo | |
151 % | |
152 % DESCRIPTION: Get Info Object | |
153 % | |
154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
155 | |
156 function ii = getInfo(varargin) | |
157 if nargin == 1 && strcmpi(varargin{1}, 'None') | |
158 sets = {}; | |
159 pl = []; | |
160 else | |
161 sets = {'Default'}; | |
162 pl = getDefaultPlist; | |
163 end | |
164 % Build info object | |
165 ii = minfo(mfilename, 'plist', 'ltpda', utils.const.categories.helper, '$Id: string.m,v 1.45 2011/04/08 08:56:21 hewitson Exp $', sets, pl); | |
166 ii.setModifier(false); | |
167 ii.setArgsmin(1); | |
168 end | |
169 | |
170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
171 % | |
172 % FUNCTION: getDefaultPlist | |
173 % | |
174 % DESCRIPTION: Get Default Plist | |
175 % | |
176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
177 | |
178 function plo = getDefaultPlist() | |
179 plo = plist(); | |
180 end | |
181 |