


STRUCTUNPACK Copies the fields of a structure into the workspace.
STRUCTUNPACK(S) creates a variable in the workspace with name
'fieldname' for each 'fieldname' that is a field of S, and copies the
value of S.('fieldname') into the variable 'fieldname'.
STRUCTUNPACK(S,1) will overwrite existing variables of name
'fieldname' without warning. STRUCTUNPACK(S,0) is equivalent to
STRUCTUNPACK(S) and will issue a warning that 'fieldname' exists but
will not overwrite the existing variable.
FIELDS = STRUCTUNPACK(S, ...) returns the fieldnames that were
successfully copied to the workspace.

0001 function names = structunpack(s, force) 0002 %STRUCTUNPACK Copies the fields of a structure into the workspace. 0003 % STRUCTUNPACK(S) creates a variable in the workspace with name 0004 % 'fieldname' for each 'fieldname' that is a field of S, and copies the 0005 % value of S.('fieldname') into the variable 'fieldname'. 0006 % 0007 % STRUCTUNPACK(S,1) will overwrite existing variables of name 0008 % 'fieldname' without warning. STRUCTUNPACK(S,0) is equivalent to 0009 % STRUCTUNPACK(S) and will issue a warning that 'fieldname' exists but 0010 % will not overwrite the existing variable. 0011 % 0012 % FIELDS = STRUCTUNPACK(S, ...) returns the fieldnames that were 0013 % successfully copied to the workspace. 0014 0015 if (nargin < 2), force = 0; end; 0016 0017 names = fieldnames(s); 0018 for n = 1:length(names) 0019 if (~force && (exist(names{n}) == 1)) % if not overwriting, detect name conflicts 0020 warning(['Field ' names{n} ' already exists in workspace. Will not copy.']); 0021 names(n) = ''; 0022 else 0023 assignin('caller', names{n}, s.(names{n})); 0024 end 0025 end 0026 0027 if (nargout == 0), clear names; end;