example, 1. [basisfile,brainfile] = vb_struct2vars(bayes_parm,{'basisfile','brainfile'}); 2. vb_struct2vars(bayes_parm,{'basisfile','brainfile'}); In the second case, the variables are created in the workspace where a 'caller' function is located. Copyright (C) 2011, ATR All Rights Reserved. License : New BSD License(see VBMEG_LICENSE.txt)
0001 function varargout = vb_struct2vars(argS, fieldname); 0002 % 0003 % example, 0004 % 1. [basisfile,brainfile] = vb_struct2vars(bayes_parm,{'basisfile','brainfile'}); 0005 % 2. vb_struct2vars(bayes_parm,{'basisfile','brainfile'}); 0006 % In the second case, the variables are created in the workspace where 0007 % a 'caller' function is located. 0008 % 0009 % Copyright (C) 2011, ATR All Rights Reserved. 0010 % License : New BSD License(see VBMEG_LICENSE.txt) 0011 0012 if ~isstruct(argS)|length(argS)~=1 0013 error('Single Input Must be a Scalar Structure.') 0014 end 0015 0016 Nfield = length(fieldname); 0017 0018 S.type = '.'; 0019 for i = 1 : Nfield 0020 if ~isfield(argS, fieldname{i}) 0021 eval(['subargS.' fieldname{i}, '=[];']); 0022 else 0023 eval(['subargS.' fieldname{i}, '=argS.', fieldname{i} ';']); 0024 end 0025 if nargout==0 % assign in caller 0026 S.subs=fieldname{i}; % for speed, inline getfield operation 0027 assignin('caller',fieldname{i},subsref(subargS,S)) 0028 else % dump into variables 0029 S.subs=fieldname{i}; % for speed, inline getfield operation 0030 varargout{i}=subsref(subargS,S); 0031 end 0032 end 0033 0034 0035