Home > database > ensemble_tree2datastruct.m

ensemble_tree2datastruct

PURPOSE ^

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

SYNOPSIS ^

function newstruct = ensemble_tree2datastruct(generic_struct,params)

DESCRIPTION ^

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

 SUPPORTED PARAMS:
    params.encapsulate_in_cells (0 or 1). Default is 0.
           This param dictates whether to encapsulate a single vector in a
           cell. Default is off. This is useful when it is not known
           ahead of time whether you will be dealing with one or more than
           one data set in your structure.

 Copyright (c) 2007-2012 The Regents of the University of California
 All Rights Reserved.

 15, Mar. 2007 - Stefan Tomic
 5 July, 2007 - Stefan Tomic, edited to conform to current
                ensemble spec. (data fields do not have to be the same length and
                their indices do not have to be associated). Consequently,
                vectors, matrices do not have to be encapsulated in
                cells. Additionally, an array of generic structs is simply converted to a
                cell array of ensemble structs
 26 June, 2008 - Stefan Tomic, arrays of structs now output as a
                 single Ensemble data struct rather than an array of Ensemble data structs
 16, Dec. 2009 - Stefan Tomic, bugfix - data are no longer encapsulated in
                 inner cells, unless you pass an array of structs. e.g.,
                  if 
                     generic_struct.fld1 = [1 2 3 4 5]
                  then
                    result_struct.vars = {'fld1'}
                    result_struct.data = {[1 2 3 4 5]}
           However if
             generic_struct(1).fld1 = [1 2 3 4 5];
             generic_struct(2).fld1 = [6 7 8 9 10];
           then
               result_struct.vars = {'fld1'}
             result_struct.data = { {[1 2 3 4 5],[6 7 8 9 10]} }

 Jan 4, 2010 - Stefan Tomic - added encapsulate_in_cells params
                              Turning this on will encapsulate
                              any vectors in a cell (even if there is a single
                              vector). Default is off (do not encapsulate
                              single vector).

 09Jan2014 Petr Janata - added 'ignore_reserved_names' option to keep
      fields with names of 'name' and 'type' in the data part of the new
      Ensemble datastruct
 19Feb2014 Petr Janata - fixed handling of situation where params is not
      passed in as an input argument

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function newstruct = ensemble_tree2datastruct(generic_struct,params)
0002 % Converts a generic data structure to conform to the ensemble data structure
0003 %
0004 % SUPPORTED PARAMS:
0005 %    params.encapsulate_in_cells (0 or 1). Default is 0.
0006 %           This param dictates whether to encapsulate a single vector in a
0007 %           cell. Default is off. This is useful when it is not known
0008 %           ahead of time whether you will be dealing with one or more than
0009 %           one data set in your structure.
0010 %
0011 % Copyright (c) 2007-2012 The Regents of the University of California
0012 % All Rights Reserved.
0013 %
0014 % 15, Mar. 2007 - Stefan Tomic
0015 % 5 July, 2007 - Stefan Tomic, edited to conform to current
0016 %                ensemble spec. (data fields do not have to be the same length and
0017 %                their indices do not have to be associated). Consequently,
0018 %                vectors, matrices do not have to be encapsulated in
0019 %                cells. Additionally, an array of generic structs is simply converted to a
0020 %                cell array of ensemble structs
0021 % 26 June, 2008 - Stefan Tomic, arrays of structs now output as a
0022 %                 single Ensemble data struct rather than an array of Ensemble data structs
0023 % 16, Dec. 2009 - Stefan Tomic, bugfix - data are no longer encapsulated in
0024 %                 inner cells, unless you pass an array of structs. e.g.,
0025 %                  if
0026 %                     generic_struct.fld1 = [1 2 3 4 5]
0027 %                  then
0028 %                    result_struct.vars = {'fld1'}
0029 %                    result_struct.data = {[1 2 3 4 5]}
0030 %           However if
0031 %             generic_struct(1).fld1 = [1 2 3 4 5];
0032 %             generic_struct(2).fld1 = [6 7 8 9 10];
0033 %           then
0034 %               result_struct.vars = {'fld1'}
0035 %             result_struct.data = { {[1 2 3 4 5],[6 7 8 9 10]} }
0036 %
0037 % Jan 4, 2010 - Stefan Tomic - added encapsulate_in_cells params
0038 %                              Turning this on will encapsulate
0039 %                              any vectors in a cell (even if there is a single
0040 %                              vector). Default is off (do not encapsulate
0041 %                              single vector).
0042 %
0043 % 09Jan2014 Petr Janata - added 'ignore_reserved_names' option to keep
0044 %      fields with names of 'name' and 'type' in the data part of the new
0045 %      Ensemble datastruct
0046 % 19Feb2014 Petr Janata - fixed handling of situation where params is not
0047 %      passed in as an input argument
0048   
0049 if nargin < 2
0050   params = struct;
0051 end
0052 
0053 if ~isfield(params,'encapsulate_in_cells') || isempty(params.encapsulate_in_cells)
0054   params.encapsulate_in_cells = 0;
0055 end
0056 
0057 if ~isfield(params,'ignore_reserved_names') || isempty(params.ignore_reserved_names)
0058   ignore_reserved_names = 0;
0059 else
0060   ignore_reserved_names = params.ignore_reserved_names;
0061 end
0062   
0063 %only perform conversion if it looks like this is not an Ensemble
0064 %data struct (i.e. the 'name','type','vars', and 'data' fields are
0065 %not present)
0066 if(all(ismember({'name','type','vars','data'},fieldnames(generic_struct))))
0067   newstruct = generic_struct;
0068   return
0069 end
0070 
0071 fnames = fieldnames(generic_struct);
0072 
0073 typeIdx = strmatch('type',fnames,'exact');
0074 nameIdx = strmatch('name',fnames,'exact');
0075 reportIdx = strmatch('report',fnames,'exact');
0076 metaIdx = strmatch('meta',fnames,'exact');
0077 
0078 %if any fieldnames are reserved fieldnames in the datastruct
0079 %(e.g. name or type), assign these to the appropriate places
0080 %instead of to 'vars' and 'data'
0081 type = ''; name=''; report = struct(); meta = struct();
0082 if ~ignore_reserved_names
0083   if(~isempty(typeIdx))
0084     fnames = setdiff(fnames,'type');
0085     type = generic_struct(1).type;
0086   end
0087   
0088   if(~isempty(nameIdx))
0089     fnames = setdiff(fnames,'name');
0090     name = generic_struct(1).name;
0091   end
0092   
0093   if(~isempty(reportIdx))
0094     fnames = setdiff(fnames,'report');
0095     report = generic_struct(1).report;
0096   end
0097   
0098   if(~isempty(metaIdx))
0099     fnames = setdiff(fnames,'meta');
0100     meta = generic_struct(1).meta;
0101   end
0102 end
0103 
0104 %if this is an array of structs, we will encapsulate the values corresponding
0105 %to each struct in a cell.
0106 if(length(generic_struct) > 1 || params.encapsulate_in_cells)
0107   encapsulate = 1;
0108 else
0109   encapsulate = 0;
0110 end
0111 
0112 for ifld = 1:length(fnames)
0113   
0114   struct_idx = 1;
0115   
0116   for struct_idx = 1:length(generic_struct)
0117     
0118     dataCell = generic_struct(struct_idx).(fnames{ifld});
0119     
0120     fieldType = class(dataCell);
0121     
0122     if(struct_idx == 1 && ifld == 1)
0123       newstruct = create_newstruct(fnames,name,type,meta,report);
0124     end
0125     
0126     switch fieldType
0127       
0128       case 'struct'
0129         if(isempty(dataCell))
0130           assignData = ensemble_init_data_struct;
0131         else
0132           assignData = ensemble_tree2datastruct(dataCell,params);
0133         end
0134         
0135       otherwise
0136         assignData = dataCell;
0137         
0138     end
0139     
0140     if(encapsulate)
0141       newstruct.data{ifld}{struct_idx,1} = assignData;
0142     else
0143       newstruct.data{ifld} = assignData;
0144     end
0145     
0146   end
0147   
0148 end
0149 
0150 return
0151 
0152 function newstruct = create_newstruct(fnames,name,type,meta,report)
0153 newstruct = ensemble_init_data_struct;
0154 newstruct.vars = fnames';
0155 newstruct.name = name;
0156 newstruct.type = type;
0157 newstruct.meta = meta;
0158 newstruct.report = report;
0159 return

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