Home > database > ensemble_extract_matrix.m

ensemble_extract_matrix

PURPOSE ^

Extracts the variable specified in params.extract_var from each of the structs in data_st.

SYNOPSIS ^

function mtx = ensemble_extract_matrix(data_st,params)

DESCRIPTION ^

 Extracts the variable specified in params.extract_var from each of the structs in data_st.
 mtx = ensemble_extract_matrix(data_st,params);

 Extracts the variable specified in params.extract_var from each of the data
 structures in data_st. If extract_var is not specified, the response_enum
 variable is returned by default.

 For example, if the data field in data_st contains a cell array of data
 structures that correspond to question IDs, ensemble_extract_matrix can be
 used to extract the response_enum vector from each of those data structures
 and return it as a single matrix.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function mtx = ensemble_extract_matrix(data_st,params)
0002 % Extracts the variable specified in params.extract_var from each of the structs in data_st.
0003 % mtx = ensemble_extract_matrix(data_st,params);
0004 %
0005 % Extracts the variable specified in params.extract_var from each of the data
0006 % structures in data_st. If extract_var is not specified, the response_enum
0007 % variable is returned by default.
0008 %
0009 % For example, if the data field in data_st contains a cell array of data
0010 % structures that correspond to question IDs, ensemble_extract_matrix can be
0011 % used to extract the response_enum vector from each of those data structures
0012 % and return it as a single matrix.
0013 
0014 % 02/04/07 Petr Janata
0015 
0016 mtx = [];
0017 
0018 % Verify that we are dealing with a valid data struct
0019 if ~is_ensemble_datastruct(data_st)
0020   fprintf('%s: Invalid top-level datastruct\n', mfilename);
0021   return
0022 end
0023 
0024 if nargin < 2 || ~isfield(params,'extract_var') || isempty(params.extract_var)
0025   extract_var = 'response_enum';
0026 else
0027   extract_var = params.extract_var;
0028 end
0029 
0030 % Check to see whether any of the variables in the top-level data struct
0031 % are themselves data structs
0032 num_ds = length(data_st.vars);
0033 data_st_mask = zeros(1,num_ds);
0034 for ids = 1:num_ds
0035   data_st_mask(ids) = is_ensemble_datastruct(data_st.data{ids});
0036 end
0037 
0038 if ~any(data_st_mask)
0039   ds_cols = set_var_col_const(data_st.vars);
0040   mtx = data_st.data{ds_cols.(extract_var)};
0041   return
0042 else
0043   for ids = 1:num_ds
0044     ds_cols = set_var_col_const(data_st.data{ids}.vars);
0045 
0046     tmp = data_st.data{ids}.data{ds_cols.(extract_var)};
0047 
0048     % Initialize the output variable
0049     if ids == 1
0050       nrows = length(tmp);
0051       if iscell(tmp)
0052         mtx = cell(nrows,num_ds);
0053       else
0054         mtx = zeros(nrows,num_ds);
0055       end
0056     end
0057 
0058     % Check to make sure that the length of the temporary vector matches the
0059     % number of rows in the output matrix
0060     if length(tmp) ~= nrows
0061       fprintf('%s: number of rows mismatch: Expected %d, found %d\n', mfilename, nrows, length(tmp));
0062       mtx = [];
0063       return
0064     end
0065 
0066     % Copy the source data into the output matrix
0067     mtx(:,ids) = tmp;
0068 
0069   end % for ids
0070 end % if ~any(data_st_mask)
0071 return
0072

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