Home > atmlab > handy > get_columns.m

get_columns

PURPOSE ^

get columns from matrix based on structure and names

SYNOPSIS ^

function [y, y_cols] = get_columns(dat, cols, fields)

DESCRIPTION ^

 get columns from matrix based on structure and names

 Select "named" columns from a matrix.  For a pair consisting of a matrix
 and a structure describing what the columns represent (such as returned
 by <a href="matlab:help
 CollocatedDataset/read">CollocatedDataset.road</a>), return a
 sub-selection of columns with an associated new describing structure.

 FORMAT

   [y, y_cols] = get_columns(data, cols_struct, fieldnames)

 IN

   data        Matrix containing data
   cols        Structure describing what's in the columns
   fieldnames:
           either:
       - Cell array of strings with columns to retain
       - Structure with fields and sub-sel of columns to retain; -1 for
       'all'

 OUT

   y           New matrix, subset of 'data'
   y_cols      Structure describing columns in 'y'

 $Id$

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

get_columns.m

SOURCE CODE ^

0001 function [y, y_cols] = get_columns(dat, cols, fields)
0002 % get columns from matrix based on structure and names
0003 %
0004 % Select "named" columns from a matrix.  For a pair consisting of a matrix
0005 % and a structure describing what the columns represent (such as returned
0006 % by <a href="matlab:help
0007 % CollocatedDataset/read">CollocatedDataset.road</a>), return a
0008 % sub-selection of columns with an associated new describing structure.
0009 %
0010 % FORMAT
0011 %
0012 %   [y, y_cols] = get_columns(data, cols_struct, fieldnames)
0013 %
0014 % IN
0015 %
0016 %   data        Matrix containing data
0017 %   cols        Structure describing what's in the columns
0018 %   fieldnames:
0019 %           either:
0020 %       - Cell array of strings with columns to retain
0021 %       - Structure with fields and sub-sel of columns to retain; -1 for
0022 %       'all'
0023 %
0024 % OUT
0025 %
0026 %   y           New matrix, subset of 'data'
0027 %   y_cols      Structure describing columns in 'y'
0028 %
0029 % $Id$
0030 
0031 i = 1;    
0032 y_cols = struct();
0033 if iscell(fields)
0034     cls = cellfun(@(X) cols.(X), fields, 'UniformOutput', false);
0035     for fni = 1:length(fields)
0036         fn = fields{fni};
0037         y_cols.(fn) = i:(i+length(cols.(fn))-1);
0038         i = i + length(cols.(fn));
0039     end
0040 elseif isstruct(fields)
0041     fns = fieldnames(fields);
0042     cls = cell(length(fns), 1);
0043     for fni = 1:length(fns)
0044         fn = fns{fni};
0045         if fields.(fn) == -1
0046             cls{i} = cols.(fn);  
0047         else
0048             cls{i} = par(cols.(fn), fields.(fn));
0049         end
0050         y_cols.(fn) = i:(i+length(cls{i})-1);
0051         i = i + length(cls{i});
0052     end
0053 else
0054     error(['atmlab:' mfilename ':invalid'], ...
0055         'Expected cell or struct, got %s, read help please', ...
0056         class(fields));
0057 end
0058 y = dat(:, horzcat(cls{:}));
0059 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005