function ncv = tsg_nc( varargin ) % tsg_nc/tsg_nc -- Constructor for "tsg_nc" class. % tsg_nc(varargin) constructs a 'tsg_nc' object from % supplied csv 'filename'. % % Usage: % nc = tsg_nc( file ) % % Example: % ncv = tsg_nc('tsg_ncvar.csv') % % Input % ------- % filename ........... csv data filename % % Output % -------- % nc ........... instance of tsg_nc object % % See also: methods('tsg_nc'). % % $Id$ % check varargin % -------------- switch nargin case 0 % create default object error('csv filename not supplied'); case 1 if( isa(varargin{1}, 'char')) file = varargin{1}; else error('Wrong input argument'); end otherwise error('Wrong number of input arguments'); end % check csv file validity % ----------------------- fid = fopen( file, 'r' ); if fid == -1 error(['The specified data file ''%s'' does not exist ...\n' ... 'or is not in the directory which is on the MATLAB search path'],... file); end % create hashtable % ---------------- data = hashtable; % read csv file % ------------- % read 2 first lines % ------------------ hdr = textscan(fid,'%s',2,'delimiter','\n','headerlines', 3 ); % extract each members from first header line % ------------------------------------------- members = textscan(hdr{1}{1},'%s','delimiter',';'); % extract variable and attribute type from second header line % ----------------------------------------------------------- types = textscan(hdr{1}{2},'%s','delimiter',';'); % generate one line format % ------------------------ format = repmat('%s', 1, numel(members{1})); % read end of file % ---------------- values = textscan(fid, format, 'delimiter',';'); % put header and members line to a hash, for next time when saving file % --------------------------------------------------------------------- header = hashtable; for i=1:numel(members{1}); header = put(header, members{1}{i}, types{1}{i}); end % populate private structure ncv % ------------------------------ ncv.file = which( file ); ncv.size = numel(values{1}); ncv.header = header; ncv.members = numel(members{1}); % loop % ---- for i=1:ncv.size % get id (first character indentifying begining & start of line (#) % ----------------------------------------------------------------- id = values{1}{i}; endl = values{ncv.members}{i}; % check if line begin and end with '#' (we add the 5 header lines) % ----------------------------------------------------------------_ if ~(strcmp(id, '#') & strcmp(endl, '#')) error('tsgqc_GUI:tsg_nc', ... 'mismatch field number in %s, line: %d\nCheck your file : %s ...', ... file, i+5, file); end % get key % ------- key = values{2}{i}; % loop over other members find in line and will give the % right matlab type to each variable attribute % ------------------------------------------------------ for j=3:ncv.members-1 member = genvarname(members{1}{j}); type = genvarname(types{1}{j}); value = values{j}{i}; % dynamically populate structure 'theStruct' % ------------------------------------------ if isempty(value) theStruct.(member) = []; else switch type case 'char' theStruct.(member) = value; % eval needed to construct matlab cell expression % ----------------------------------------------- case 'cell' theStruct.(member) = eval(['{' value '}']); case 'byte' theStruct.(member) = int8(str2num(value)); case 'integer' theStruct.(member) = int16(str2num(value)); case 'float' theStruct.(member) = single(str2double(value)); case 'double' theStruct.(member) = str2double(value); otherwise % by default, display a warning % ----------------------------- warning('tsgqc_GUI:tsg_nc', ... 'unknow type ''%s'' for ''%s'' attribute.\nCheck your %s file', ... type, member, ncv.file); % and cast to char % ---------------- theStruct.(member) = value; end end end % populate the hashtable % ---------------------- data = put(data, key, theStruct); % initialize internal structure for next loop % ------------------------------------------- theStruct = []; end % remove id, first member (key) and endl in structure % --------------------------------------------------- ncv.members = ncv.members - 3; % bless tsg_nc class and inherite from hashtable data % --------------------------------------------------- ncv = class(ncv, 'tsg_nc', data ); % close file % ---------- fclose(fid);