Home > . > copyfields.m

# copyfields

## PURPOSE

'copyfields' copy fields between two matlab structures

## SYNOPSIS

function NewData=copyfields(listfields,SourceData,OldData)

## DESCRIPTION

    'copyfields' copy fields between two matlab structures
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     OUPUT:
     NewData: resulting structure

    INPUT:
     listfields: cell arrays representing the list of field names to be copied
     SourceData: structure containing the source data to copy in NewData
     OldData: (optional) preexisting data structure.

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SOURCE CODE

    0001 %'copyfields' copy fields between two matlab structures
    0002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0003 % OUPUT:
    0004 % NewData: resulting structure
    0005 %
    0006 %INPUT:
    0007 % listfields: cell arrays representing the list of field names to be copied
    0008 % SourceData: structure containing the source data to copy in NewData
    0009 % OldData: (optional) preexisting data structure.
    0010
    0011 function NewData=copyfields(listfields,SourceData,OldData)
    0012 if ~exist('OldData','var')
    0013     OldData=[];
    0014 end
    0015 NewData=OldData;%default
    0016 for ifield=1:length(listfields)
    0017     if isfield(SourceData,listfields{ifield}) & ~isempty(eval(['SourceData.' listfields{ifield}]))
    0018         eval(['NewData.' listfields{ifield} '=SourceData.' listfields{ifield} ';']);
    0019     elseif isfield(OldData,listfields{ifield})
    0020         NewData=rmfield(NewData,listfields{ifield});
    0021     end
    0022 end