function write(self, varargin) % write dynaload memory structure to ASCII CSV file (Comma Separated Value) % nc is a dynaload objet in memory load from xls file. for example: % >> nc = dynaload('test.xls'); % >> nc % dynaload % % file: test.xls % % DIMENSIONS hashtable % VARIABLES hashtable % ATTRIBUTES hashtable % % >> s = nc.VARIABLES.SSTP % % s = % code: 'SSTP' % dimension: {'DAYD'} % nctype: 'float' % long_name: 'SEA SURFACE TEMPERATURE' % standard_name: 'surface temperature' % units: 'degrees Celsius' % conventions: '' % valid_min: -1.5000 % valid_max: 38 % format: '%6.3lf' % FillValue_: 99999 % epic_code: [] % axis: '' % resolution: 1.0000e-003 % comment: [1x99 char] % missing_value: [] % % write(nc, 'foo.csv'); % % >> type foo.csv % % datagui_profile_netcdf.csv: 0 members & 0 lines with data; VERSION = 0.000 % % $DIMENSIONS$ % #;code;value;# % #;char;integer;# % #;PROFILE;120;# % ... % % % $VARIABLES$ % #;code;dimension;nctype;long_name;standard_name;units;conventions;valid_min;valid_max;format;FillValue_;epic_code;axis;resolution;comment;missing_value;# % #;char;cell;char;char;char;char;char;double;double;char;double;double;char;double;char;double;# % #;REFERENCE_DATE_TIME;'STRING14';char;REFERENCE DATE TIME FOR JULIAN DAYS;reference date time;;yyyymmddhhmmss;;;;;;;;Reference date for julian days origin;;# % ... % % $Id$ % slip filename into path, name & extension % ----------------------------------------- [path, name, ext] = fileparts(self.file); % if a second arg is a file name % ----------------------------- if isa(varargin{1}, 'char') path_ = fileparts(varargin{1}); if isempty(path_) self.file = fullfile(path,varargin{1}); else self.file = varargin{1}; end elseif strcmpi(ext, '.xls') % otherwise change xls extension to csv % ------------------------------------- [path, file, ext] = fileparts(regexprep(self.file, '.xls', '.csv')); self.file = strcat(path, file, ext); end % open csv file % --------------- self.fid = fopen(self.file, 'wt'); if self.fid == -1 error(['Can''t open file: ' self.file ' in write mode']) end % display information on command window % ------------------------------------- fprintf('Write data file: %s\n', self.file); % write comment information % ------------------------- fprintf( self.fid, '%% %s: %d members & %d lines with data; VERSION = %5.3f\n',... self.file, 0, 0, 0) ; fprintf( self.fid, '%% $Id$\n%%\n'); % write information % ------------------------ for k = keys(self) fprintf( self.fid, '%%\n$%s$\n', char(k)); % write header lk = keys(get(self, k)); s = get(get(self, k), lk{1}); % initialise str % -------------- str = '#;'; str2 = '#;'; for m = fieldnames(s)' str = strcat(str, sprintf('%s;', char(m))); value = s.(char(m)); if isa(value,'logical') str2 = strcat(str2, 'logical;'); elseif isa(value,'char') str2 = strcat(str2, 'char;'); elseif isa(value,'integer') str2 = strcat(str2, 'integer;'); elseif isa(value,'single') str2 = strcat(str2, 'float;'); elseif isa(value,'double') str2 = strcat(str2, 'double;'); elseif iscell(value) str2 = strcat(str2, 'cell;'); else error('dynaload.write: undefine %s value for member %s', .... char(value), m); end end str = strcat(str, '#'); str2 = strcat(str2, '#'); fprintf( self.fid, '%s\n', str); fprintf( self.fid, '%s\n', str2); % write data lines for nk = keys(get(self, k)) s = get(get(self, k), nk); str = '#;'; for m = fieldnames(s)' value = s.(char(m)); if islogical(value) theFormat = '%d'; elseif iscell(value) theFormat = '%s'; elseif isa(value,'char') theFormat = '%s'; % else isa(value,'uint8') % theFormat = '%u'; elseif isa(value,'integer') theFormat = '%d'; % format %g is used for scientific format 1e36 and %8.8g to write 99999.999 elseif isa(value,'single') theFormat = '%8.8g'; elseif isa(value,'double') theFormat = '%8.8g'; else error('dynaload.write: undefine %s value for member %s', .... char(value), m); end if isempty(value) str = strcat(str, ';'); else if iscell(value) % if iscell, write 'val', or 'val1','val2', etc.... for c = value % if it is last cell, add ; separator if strcmp(c, value{size(value,2)}) str = strcat(str, sprintf('''%s'';', char(c))); else % add comma separator str = strcat(str, sprintf('''%s'',', char(c))); end end else % use strtrim to remove blank (format %8.8g) % ------------------------------------------ str = strcat(str, strtrim(sprintf(strcat(theFormat, ';'), value))); end end end str = strcat(str, '#'); fprintf( self.fid, '%s\n', str); end end % add comment line at the end of file % ----------------------------------- fprintf( self.fid, '%%\n', str); % close file % ---------- fclose(self.fid); end