Home > database > archived_scripts > ensemble_tree2datastruct_bak.m

ensemble_tree2datastruct_bak

PURPOSE ^

SYNOPSIS ^

function newstruct = ensemble_tree2datastruct(generic_struct,params)

DESCRIPTION ^


 converts a generic data structure to conform to the ensemble data
 structure

 params struct isn't used yet. This might be useful for specifying
 the type and names fields of the data struct but 

 15, Mar. 2007 - Stefan Tomic

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function newstruct = ensemble_tree2datastruct(generic_struct,params)
0002 %
0003 %
0004 % converts a generic data structure to conform to the ensemble data
0005 % structure
0006 %
0007 % params struct isn't used yet. This might be useful for specifying
0008 % the type and names fields of the data struct but
0009 %
0010 % 15, Mar. 2007 - Stefan Tomic
0011 
0012 %only perform conversion if it looks like this is not an Ensemble
0013 %data struct (i.e. the 'name','type','vars', or 'data' fields are
0014 %not present)
0015 if(all(ismember({'name','type','vars','data'},fieldnames(generic_struct))))
0016   newstruct = generic_struct;
0017   return
0018 end
0019 
0020 fnames = fieldnames(generic_struct);
0021 
0022 typeIdx = strmatch('type',fnames,'exact');
0023 nameIdx = strmatch('name',fnames,'exact');
0024 newstruct = ensemble_init_data_struct;
0025 
0026 %if any fieldnames are reserved fieldnames in the datastruct
0027 %(e.g. name or type), assign these to the appropriate places
0028 %instead of to 'vars' and 'data'
0029 if(~isempty(typeIdx))
0030   fnames = setdiff(fnames,fnames{typeIdx});
0031   newstruct.type = generic_struct(1).type;
0032 end
0033 
0034 if(~isempty(nameIdx))
0035   fnames = setdiff(fnames,fnames{nameIdx});
0036   newstruct.name = generic_struct(1).name;
0037 end
0038 
0039 newstruct.vars = fnames';
0040 
0041 for ifld = 1:length(fnames)
0042  
0043   fieldType = '';
0044   struct_idx = 1;
0045   %have to determine the type of field this is by inspecting the
0046   %first non-empty value
0047   while(isempty(fieldType) & struct_idx <= length(generic_struct))
0048     dataCell = generic_struct(struct_idx).(fnames{ifld});
0049     if(~isempty(dataCell))
0050       if(isnumeric(dataCell) & (length(dataCell(:)) == 1))
0051     fieldType = 'numeric';
0052       elseif(isnumeric(dataCell) & (length(dataCell(:)) > 1))
0053     fieldType = 'matrix';
0054       elseif(isstruct(dataCell))
0055     fieldType = 'struct';
0056       elseif(ischar(dataCell))
0057     fieldType = 'char';
0058       elseif(iscell(dataCell))
0059     fieldType = 'cell';
0060       end
0061     end
0062     struct_idx = struct_idx + 1;
0063   end
0064   
0065   %default the fieldType to cell if no data was found
0066   if(isempty(fieldType))
0067     fieldType = 'cell';
0068   end
0069   
0070   for struct_idx = 1:length(generic_struct)
0071   
0072     dataCell = generic_struct(struct_idx).(fnames{ifld});
0073 
0074     switch fieldType
0075     
0076      case 'numeric'
0077       if(isempty(dataCell))
0078     assignData = NaN;
0079       else
0080     assignData = dataCell;
0081       end
0082     
0083      case 'struct'
0084       if(isempty(dataCell))
0085     assignData = {ensemble_init_data_struct};
0086       else
0087     assignData = {ensemble_tree2datastruct(dataCell)};
0088       end
0089     
0090      case 'char'
0091       if(isempty(dataCell))
0092     assignData = {''};
0093       else
0094     assignData = {dataCell};
0095       end
0096       
0097      case {'cell','matrix'}
0098       if(isempty(dataCell))
0099     assignData = {[]};
0100       else
0101     assignData = {dataCell};
0102       end
0103     
0104     end
0105     
0106     newstruct.data{1,ifld}(struct_idx,1) = assignData; 
0107     
0108   end
0109     
0110   
0111 
0112 end
0113

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003