Code covered by the BSD License  

Highlights from
inputgui

inputgui

by

 

08 May 2003 (Updated )

A simple, flexible, and overridable graphical replacement for INPUT.

inputgui(name,default,prompt,isstring,action,...
function value = inputgui(name,default,prompt,isstring,action,...
                          Title,LineNo,varargin)

%INPUTGUI Allow a user to set parameters through a dialog.
%   INPUTGUI allows a user to set function parameters through an input
%   dialog window. The standard calling sequence is
%
%     value = INPUTGUI(name,default,prompt)
%
%   Arguments name, default, and prompt are identically sized cell
%   arrays of strings. They define the names, default values, and
%   interactive prompt strings for the parameters, which are assumed
%   to be numerical.
%
%   On exit, each parameter value will be assigned *in the caller's
%   workspace*. The return argument "value" will be a cell array of
%   the assigned values. 
%
%   INPUTGUI(name,default,prompt,isstring) adds a logical vector
%   that designates corresponding parameters as strings.
%
%   INPUTGUI(...,action) modifies the behavior:
%     'default'   Default values are returned for all parameters (no
%                 dialog is opened). 
%     struct      Each parameter will have the default value unless 
%                 overridden by a field of the same name (no dialog).
%     []          Standard behavior.
%
%   INPUTGUI(...,Title,LineNo,AddOpts) allows passing through
%   additional arguments understood by INPUTDLG.
%   
%   For example, suppose you wrote the function
%
%    function y = inputtest(varargin)
%    name = { 'Moe', 'Larry', 'Curly' };
%    default = { '1/pi', '[-1 0 1]', 'silly' };
%    prompt = { 'Value of Moe', 'Value of Larry', 'Value of Curly' };
%    isstring = logical( [0 0 1] );
%    vals = inputgui(name,default,prompt,isstring,varargin{:})
%    whos Moe Larry Curly
%
%   Then we get
%   >> inputtest   % Brings up a dialog. Press OK and...
%   vals = 
%   
%       [    0.3183]
%       [1x3 double]
%       'silly'
%   
%     Name        Size                   Bytes  Class
%     Curly       1x5                       10  char array
%     Larry       1x3                       24  double array
%     Moe         1x1                        8  double array
%
%   >> s.Larry = 16; inputtest(s)  % No dialog appears...
%   vals = 
%   
%       [0.3183]
%       [    16]
%       'silly'
%   
%     Name        Size                   Bytes  Class
%     Curly       1x5                       10  char array
%     Larry       1x1                        8  double array
%     Moe         1x1                        8  double array
%
%   See also, INPUTDLG, ASSIGNIN.
%   
%   Copyright (c) 2003 by Toby Driscoll (driscoll@math.udel.edu).
%   All rights reserved. Free for non-commercial use.

% Parse & check.
N = length(name);

if nargin < 7
  LineNo = 1;  
  if nargin < 6
    Title = 'Set parameters';
    if nargin < 5
      action = [];
      if nargin < 4
        isstring = [];
      end
    end
  end
end
  
if length(default)~=N | length(prompt)~=N | length(isstring)~=N
  error('Mismatched data lengths.')
end

if isempty(isstring)
   isstring = false(N,1);
elseif length(isstring)==1
  isstring = repmat(isstring,N,1);
end

value = default;

% Run dialog if necessary. Note that an empty struct means default
% values, no dialog.
if isempty(action) && ~isstruct(action)
  value = inputdlg(prompt,Title,LineNo,value,varargin{:});
  if isempty(value)
    error('Parameter input cancelled.')
  end
end

% Write values.
for k = 1:N
  if isfield( action, name{k} )
    val =  action.(name{k});
  else
    val = value{k};
  end
  if ~isstring(k) && isstr(val)
    % (Note: val may not have been str if it came from the struct.)
    val = str2num(val); 
  end
  assignin( 'caller', name{k}, val)
  % Update the output argument.
  value{k} = val;
end

value = value(:);

Contact us