from
keepfield
by Brett Shoelson
Keep specified fields of a structure, discarding other fields.
|
| keepfield(instruct,fields)
|
function outstruct = keepfield(instruct,fields)
% OUTSTRUCT = KEEPFIELD(INSTRUCT,FIELDNAMES)
% Removes all fields from a structure other than those specified in
% input variable FIELDS.
%
% FIELDS may be a single field, or a cell array of field names
%
% ex:
% a = dir('*.m');
% b = keepfield(a,{'name','bytes'});
%
% See also: rmfield
%
% Written by Brett Shoelson, PhD
% 3/1/05
% shoelson@helix.nih.gov
if nargin ~= 2
error('Requires 2 input arguments.');
end
if ~isa(instruct,'struct')
error('The first input argument must be a structure.');
end
if (~isa(fields,'char') & ~isa(fields,'cell')) | (isa(fields,'cell') & ~all(cellfun('isclass',fields,'char')))
error('The second input argument must be a string representing the name of a field to keep or a cell array of field names to keep.');
end
outstruct = rmfield(instruct,setdiff(fieldnames(instruct),fields));
|
|
Contact us