comparison m-toolbox/classes/@ltpda_uo/save.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 % SAVE overloads save operator for ltpda objects.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: SAVE overloads save operator for ltpda objects.
5 %
6 % CALL: save(obj, 'blah.mat') Save an object obj as a .mat file.
7 % obj.save('blah.mat') Save an object obj as a .mat file.
8 % obj.save(plist('filename', 'blah.mat'))
9 % save(obj, plist('filename', 'blah.mat'))
10 % save(a, 'blah.xml') Save an object as an XML file.
11 % a.save(plist('filename', 'blah.xml'))
12 %
13 % The method accepts multiple input objects (in a list or in a vector),
14 % that will be save inside a single file or in multiple files according to the
15 % "INDIVIDUAL FILES" parameter (see the Parameters Description below)
16 %
17 % <a href="matlab:utils.helper.displayMethodInfo('ltpda_uo', 'save')">Parameters Description</a>
18 %
19 % VERSION: $Id: save.m,v 1.41 2011/10/05 09:49:31 ingo Exp $
20 %
21 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
22
23 function varargout = save(varargin)
24
25 %%% Check if this is a call for parameters
26 if utils.helper.isinfocall(varargin{:})
27 varargout{1} = getInfo(varargin{3});
28 return
29 end
30
31 import utils.const.*
32 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
33
34 %%% Collect input variable names
35 in_names = cell(size(varargin));
36 for ii = 1:nargin,in_names{ii} = inputname(ii);end
37
38 [objs, objinvars, rest] = utils.helper.collect_objects(varargin(:), '', in_names);
39 [pls, invars, rest] = utils.helper.collect_objects(rest(:), 'plist', in_names);
40
41 %%% REMARK: Special case for the plist-class because collect_objects collects
42 %%% ALL plist-objects even the plist which should set the property.
43 %%% In this case must be the plist which sets thte property
44 %%% at the last position.
45 if isa(objs, 'plist')
46 if nparams(objs(end)) == 1 && isparam(objs(end), 'filename')
47 pls = [pls objs(end)];
48 objs(end) = [];
49 end
50 end
51
52 %%% Combine the plists
53 pls = combine(pls, getDefaultPlist());
54
55 %%%
56 % 1. Use the filename from the PLIST
57 % 2. Use the input string as the filename
58 % 3. Use the object name and the current folder for the filename
59 % Must be defined for each object.
60 % 4. If there are more than one input objects and the 'individual files'
61 % Switch is false then use the variable name.
62 filename = '';
63 if ~isempty(pls.find('filename'))
64 filename = pls.find('filename');
65 elseif ~isempty(rest) && numel(rest) == 1 && iscellstr(rest)
66 filename = rest{1};
67 pls.pset('filename', filename);
68 end
69
70 %%% Make sure that the UUID is set for all objects. This should only
71 %%% happen for PLISTs.
72 %%% REMARK: This command will also change the plist in the workspace.
73 for ii = 1:numel(objs)
74 if isempty(objs(ii).UUID)
75 objs(ii).UUID = char(java.util.UUID.randomUUID);
76 end
77 end
78
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Save object %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80
81 % Inspect filename
82 [path, fname, ext] = fileparts(filename);
83
84 % Save the objects as MAT files if the user doesn't specify a extension type
85 if isempty(ext)
86 ext = '.mat';
87 end
88
89 % Get pre-, and postfix from the input PLIST
90 prefix = pls.find('prefix');
91 postfix = pls.find('postfix');
92
93 individualFiles = pls.find('individual files');
94
95 % ATTENTION: We keep the meaning of t0 for backwars compatibility.
96 % This means
97 % - before saving, t0 = t0 + toffset
98 % - after loading, t0 = t0 - toffset
99 % But be careful. For XML files it is done in the tsdata
100 % methods 'attachToDom' and 'fromDom' because for submitting
101 % we don't use this method.
102
103 switch ext
104 case '.mat'
105
106 % ATTENTION: We moved the changing to the t0 to the MATLAB methods:
107 % tsdata/loadobj and tsdata/saveobj
108
109 objsout = objs;
110
111 v = ver('MATLAB');
112 % MATLAB version number of 2008b is 7.7
113 if utils.helper.ver2num(v.Version) < utils.helper.ver2num('7.7')
114
115 warning('off', 'all')
116 shape = size(objs);
117 objs = utils.prog.rstruct(objs);
118 objs = reshape(objs, shape);
119 warning('on', 'all')
120 else
121 end
122
123 if (individualFiles == true)
124
125 %%%%% Save each object in individual file
126 dummy = objs;
127 for ii = 1:numel(dummy)
128 objs = dummy(ii);
129 % Define full filename
130 if isempty(fname)
131 fullFilename = getFullFilename(objs.name);
132 else
133 postfix = sprintf('%s_%03d', pls.find('postfix'), ii);
134 fullFilename = getFullFilename(fname);
135 end
136 save(fullFilename, 'objs');
137 end
138
139 else
140
141 %%%%% Save all objects in one file
142 if isempty(fname)
143 fullFilename = getFullFilename(inputname(1));
144 warning('!!! You have not specified any file name -> Using first variable name as file name.');
145 else
146 fullFilename = getFullFilename(fname);
147 end
148
149 save(fullFilename, 'objs');
150 objs = objsout;
151 end
152
153 case '.xml'
154
155 if (individualFiles == true)
156 %%%%% Save each object in individual file
157
158 for ii = 1:numel(objs)
159 if isempty(fname)
160 fullFilename = getFullFilename(objs(ii).name);
161 else
162 postfix = sprintf('%s_%03d', pls.find('postfix'), ii);
163 fullFilename = getFullFilename(fname);
164 end
165 saveObjectAsXML(objs(ii), fullFilename);
166 end
167
168 else
169 %%%%% Save all objects in one file
170 if isempty(fname)
171 fullFilename = getFullFilename(inputname(1));
172 warning('!!! You have not specified any file name -> Using first variable name as file name.');
173 else
174 fullFilename = getFullFilename(fname);
175 end
176
177 saveObjectAsXML (objs, fullFilename);
178 end
179
180
181 otherwise
182 error('### unknown file extension [%s].', ext);
183 end
184
185 varargout{1} = objs;
186
187 %--------------------------------------------------------------------------
188 % Return the full file name
189 %--------------------------------------------------------------------------
190 function fullFilename = getFullFilename(fname)
191
192 % concatenate the prefix, filename, postfix and the file extension.
193 fname = strcat(prefix, fname, postfix, ext);
194
195 % build full filename
196 fullFilename = fullfile(path, fname);
197
198 end
199
200 end
201
202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203 % Local Functions %
204 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205
206 %--------------------------------------------------------------------------
207 % save object as XML file
208 %--------------------------------------------------------------------------
209 function saveObjectAsXML (obj, fullFilename)
210
211 % Create DOM node
212 dom = com.mathworks.xml.XMLUtils.createDocument('ltpda_object');
213 parent = dom.getDocumentElement;
214
215 % add Attribute 'ltpda_version' to the root node
216 ltpda_version = getappdata(0, 'ltpda_version');
217 parent.setAttribute('ltpda_version', ltpda_version);
218
219 if (utils.helper.ver2num(ltpda_version) > utils.helper.ver2num('2.3')) || ...
220 (strcmp(strtok(ltpda_version), '2.3'))
221 %%%%%%%%%%%%%%%%%% saving of a new XML file %%%%%%%%%%%%%%%%%%
222
223 % Create history root node
224 % The attachToDom methods will attach their histories to this node.
225 historyRootNode = dom.createElement('historyRoot');
226 parent.appendChild(historyRootNode);
227
228 % Write objects
229 obj.attachToDom(dom, parent, []);
230
231 else
232 %%%%%%%%%%%%%%%%%% saving of a old XML file %%%%%%%%%%%%%%%%%%
233 utils.xml.xmlwrite(obj, dom, parent, ''); % Save the XML document.
234 end
235
236 % Write to file
237
238 % Ingo: I want to use our own XML write method because I miss on my
239 % machine the indent.
240 if isempty(strfind(fullFilename, filesep))
241 result = javax.xml.transform.stream.StreamResult(fullfile(pwd, fullFilename));
242 else
243 result = javax.xml.transform.stream.StreamResult(fullFilename);
244 end
245 mpipeline.utils.XMLUtils.serializeXML(dom, result, 'UTF-8')
246 % xmlwrite(fullFilename, dom);
247
248 end
249
250
251 %--------------------------------------------------------------------------
252 % Get Info Object
253 %--------------------------------------------------------------------------
254 function ii = getInfo(varargin)
255 if nargin == 1 && strcmpi(varargin{1}, 'None')
256 sets = {};
257 pl = [];
258 else
259 sets = {'Default'};
260 pl = getDefaultPlist();
261 end
262 % Build info object
263 ii = minfo(mfilename, 'ltpda_uo', 'ltpda', utils.const.categories.output, '$Id: save.m,v 1.41 2011/10/05 09:49:31 ingo Exp $', sets, pl);
264 ii.setOutmin(0);
265 end
266
267 %--------------------------------------------------------------------------
268 % Get Default Plist
269 %--------------------------------------------------------------------------
270 function plout = getDefaultPlist()
271 persistent pl;
272 if ~exist('pl', 'var') || isempty(pl)
273 pl = buildplist();
274 end
275 plout = pl;
276 end
277
278 function pl = buildplist()
279 % General plist for saving objects
280 pl = plist.SAVE_OBJ_PLIST;
281
282 end
283