| 1 | %'copyfields' copy fields between two matlab structures |
|---|
| 2 | %------------------------------------------------------------------------ |
|---|
| 3 | % OUTPUT: |
|---|
| 4 | % NewData: resulting structure |
|---|
| 5 | % |
|---|
| 6 | % INPUT: |
|---|
| 7 | % listfields: cell arrays representing the list of field names to be copied |
|---|
| 8 | % SourceData: structure containing the source data to copy in NewData |
|---|
| 9 | % OldData: (optional) preexisting data structure. |
|---|
| 10 | |
|---|
| 11 | %======================================================================= |
|---|
| 12 | % Copyright 2008-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
|---|
| 13 | % http://www.legi.grenoble-inp.fr |
|---|
| 14 | % Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr |
|---|
| 15 | % |
|---|
| 16 | % This file is part of the toolbox UVMAT. |
|---|
| 17 | % |
|---|
| 18 | % UVMAT is free software; you can redistribute it and/or modify |
|---|
| 19 | % it under the terms of the GNU General Public License as published |
|---|
| 20 | % by the Free Software Foundation; either version 2 of the license, |
|---|
| 21 | % or (at your option) any later version. |
|---|
| 22 | % |
|---|
| 23 | % UVMAT is distributed in the hope that it will be useful, |
|---|
| 24 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 25 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 26 | % GNU General Public License (see LICENSE.txt) for more details. |
|---|
| 27 | %======================================================================= |
|---|
| 28 | |
|---|
| 29 | function NewData=copyfields(listfields,SourceData,OldData) |
|---|
| 30 | if ~exist('OldData','var') |
|---|
| 31 | OldData=[]; |
|---|
| 32 | end |
|---|
| 33 | NewData=OldData;%default |
|---|
| 34 | for ifield=1:length(listfields) |
|---|
| 35 | if isfield(SourceData,listfields{ifield}) & ~isempty(eval(['SourceData.' listfields{ifield}])) |
|---|
| 36 | eval(['NewData.' listfields{ifield} '=SourceData.' listfields{ifield} ';']); |
|---|
| 37 | elseif isfield(OldData,listfields{ifield}) |
|---|
| 38 | NewData=rmfield(NewData,listfields{ifield}); |
|---|
| 39 | end |
|---|
| 40 | end |
|---|