function out = GUIStructure(inStruct, varargin) % NOTE: Since only the input structure (usually) is passed to this function, % 'varargin' will usally be empty, regardless of the number of fields in the % inStruct. The only reason to send additional arguments to this function would % be to override the default values for 'prompt', 'title', 'lineNo', and 'defs', % (which are defined below). % Code written by David Alais. % number of fields and number of variable arguments nFields = length(struct2cell(inStruct)); nArg = length(varargin); % usually zero. % set defaults prompt = fieldnames(inStruct); % field names to be shown in GUI prompt. title = 'Parameters'; % Title for GUI window. lineNo = 2; % number of lines in each field of the GUI window. defs = struct2cell(inStruct); % values to be shown in each field of the GUI window. % identify non-numeric fields isCharField = zeros(nFields, 1); for i=1:nFields isCharField(i) = ischar(defs{i}); end % temporarily convert numeric data to string data to be pasted in fields of GUI window. for i=1:nFields if ~isCharField(i) defs{i} = num2str(defs{i}); end end % overide defaults (usually, 'nArg' will be zero) if nArg > 0 prompt = varargin{1}; if nArg > 1 title = varargin{2}; if nArg > 2 lineNo = varargin{3}; if nArg > 3 defs = varargin{4}; if nArg > 4 error('Too many input parameters'); end end end end end % create input dialog box to get user input % (prompt is a cell array containing strings - the field names) % (defs are the numerical values for each field, in string format) out = inputdlg(prompt, title, lineNo, defs); options.Resize='on'; options.WindowStyle='normal'; options.Interpreter='none'; % convert parameter values from string fields back to numeric format for i=1:nFields if ~isCharField(i) out{i} = str2num(out{i}); end end % convert cell array back to structure before returning out = cell2struct(out, fieldnames(inStruct));