Home > atmlab > handy > getfields.m

getfields

PURPOSE ^

% getfields Get multiple fields from structure

SYNOPSIS ^

function S = getfields(S, varargin)

DESCRIPTION ^

% getfields Get multiple fields from structure

 This is like getfield, but accepts multiple fieldnames.

 IN

   S       structure
   fields  Fields to be returned

 OUT

   S       new structure

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

getfields.m

SOURCE CODE ^

0001 function S = getfields(S, varargin)
0002 %% getfields Get multiple fields from structure
0003 %
0004 % This is like getfield, but accepts multiple fieldnames.
0005 %
0006 % IN
0007 %
0008 %   S       structure
0009 %   fields  Fields to be returned
0010 %
0011 % OUT
0012 %
0013 %   S       new structure
0014 
0015 % $Id$
0016 
0017 if isempty(varargin)
0018     S = struct();
0019     return
0020 end
0021 
0022 if iscell(varargin{1})
0023     fields = varargin{1};
0024 else
0025     fields = varargin;
0026 end
0027 
0028 S = cell2struct(cellfun(@(field) S.(field), vec2row(fields), 'UniformOutput', false), ...
0029                 vec2row(fields), ...
0030                 2);
0031 
0032 end

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