


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.
See also STRUCTPACK.

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 % See also STRUCTPACK. 0016 0017 if (nargin < 2), force = 0; end; 0018 0019 names = fieldnames(s); 0020 for n = 1:length(names) 0021 if (~force && (evalin('caller', ['exist(''' names{n} ''')']) == 1)) % if not overwriting, detect name conflicts 0022 warning(['Field ' names{n} ' already exists in workspace. Will not copy.']); 0023 else 0024 assignin('caller', names{n}, s.(names{n})); 0025 end 0026 end 0027 0028 if (nargout == 0), clear names; end;