comparison m-toolbox/classes/@ltpda_uoh/csvexport.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 % CSVEXPORT Exports the data of an object to a csv file.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % FUNCTION: csvexport
5 %
6 % DESCRIPTION: Exports the data of an object to a csv file.
7 %
8 % CALL: csvexport(in-objects)
9 %
10 % INPUTS: in-objects: Input objects which data should be stored to
11 % disc.
12 %
13 % OUTPUTS: success: If storing of the data was successful
14 %
15 % <a href="matlab:utils.helper.displayMethodInfo('ltpda_uoh', 'csvexport')">Parameters Description</a>
16 %
17 % VERSION: $Id: csvexport.m,v 1.7 2011/04/08 08:56:30 hewitson Exp $
18 %
19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20
21 function varargout = csvexport(varargin)
22
23 %%% Check if this is a call for parameters
24 if utils.helper.isinfocall(varargin{:})
25 varargout{1} = getInfo(varargin{3});
26 return
27 end
28
29 [objs, obj_invars, rest] = utils.helper.collect_objects(varargin(:), '');
30 pli = utils.helper.collect_objects(rest(:), 'plist');
31
32 [data, plData] = csvGenerateData(objs);
33
34 pl = combine(pli, getDefaultPlist);
35
36 filename = pl.find('filename');
37 commentChar = pl.find('commentChar');
38 description = pl.find('description');
39
40 % Some plausibility checks
41 if isempty(filename)
42 if ~isempty(rest) && ischar(rest{1})
43 filename = rest{1};
44 pl.pset('filename', filename);
45 else
46 error('### No filename is specified');
47 end
48 end
49 if isempty(description)
50 description = plData.find('DESCRIPTION');
51 end
52 columns = plData.find('COLUMNS');
53 nrows = plData.find('NROWS');
54 ncols = plData.find('NCOLS');
55 objIDs = plData.find('OBJECT IDS');
56 objNames = plData.find('OBJECT NAMES');
57 creator = plData.find('CREATOR');
58 created = format(time(), 'yyyy-mm-dd HH:MM');
59
60 fid = fopen(filename, 'w');
61
62 % Check fid
63 if fid == -1
64 error('### Can not open the file: %s', filename);
65 end
66
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 %%% write header %%%
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70
71 fprintf(fid, '%s\n', commentChar);
72 fprintf(fid, '%s DESCRIPTION: %s\n', commentChar, description);
73 fprintf(fid, '%s\n', commentChar);
74 fprintf(fid, '%s COLUMNS: %s\n', commentChar, columns);
75
76 columnNames = strtrim(regexp(columns, ',', 'split'));
77 maxColNames = max(cellfun(@length, columnNames));
78
79 for ii = 1:numel(columnNames)
80 columnDesc = plData.find(columnNames{ii});
81 if ~isempty(columnDesc)
82 off = '';
83 off(1:maxColNames-length(columnNames{ii})) = ' ';
84 fprintf(fid, '%s %s%s: %s\n', commentChar, columnNames{ii}, off, columnDesc);
85 end
86 end
87
88 fprintf(fid, '%s\n', commentChar);
89 fprintf(fid, '%s NROWS: %d\n', commentChar, nrows);
90 fprintf(fid, '%s NCOLS: %d\n', commentChar, ncols);
91 fprintf(fid, '%s OBJECT IDS: %s\n', commentChar, objIDs);
92 fprintf(fid, '%s OBJECT NEAMES: %s\n', commentChar, objNames);
93 fprintf(fid, '%s\n', commentChar);
94 fprintf(fid, '%s CREATION DATE: %s\n', commentChar, created);
95 fprintf(fid, '%s\n', commentChar);
96 fprintf(fid, '%s CREATED BY: %s\n', commentChar, creator);
97 fprintf(fid, '%s\n', commentChar);
98 fprintf(fid, '%s\n', commentChar);
99
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 %%% write data %%%
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103
104 for nn=1:nrows
105 for dd = 1:ncols
106 if numel(data{dd}) >= nn
107 fprintf(fid, '%.17f', data{dd}(nn));
108 end
109 if dd < numel(data)
110 fprintf(fid, ',');
111 end
112 end
113 fprintf(fid, '\n');
114 end
115
116 fclose(fid);
117
118 end
119
120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 % Local Functions %
122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123
124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 %
126 % FUNCTION: getInfo
127 %
128 % DESCRIPTION: Get Info Object
129 %
130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131
132 function ii = getInfo(varargin)
133 if nargin == 1 && strcmpi(varargin{1}, 'None')
134 sets = {};
135 pl = [];
136 else
137 sets = {'Default'};
138 pl = getDefaultPlist;
139 end
140 % Build info object
141 ii = minfo(mfilename, 'ltpda_uoh', 'ltpda', utils.const.categories.helper, '$Id: csvexport.m,v 1.7 2011/04/08 08:56:30 hewitson Exp $', sets, pl);
142 end
143
144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 %
146 % FUNCTION: getDefaultPlist
147 %
148 % DESCRIPTION: Get Default Plist
149 %
150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151
152 function plout = getDefaultPlist()
153 persistent pl;
154 if exist('pl', 'var')==0 || isempty(pl)
155 pl = buildplist();
156 end
157 plout = pl;
158 end
159
160 function plo = buildplist()
161 plo = plist();
162 p = param({'filename' 'cvs filename.'}, '');
163 plo.append(p);
164 p = param({'commentChar','The comment character in the file.'}, {1, {'#'}, paramValue.OPTIONAL});
165 plo.append(p);
166 p = param({'description', 'Description for the file.'}, '');
167 plo.append(p);
168 end
169
170
171