1 ## Copyright (C) 2010 Martin Hepperle
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function file} {@var{P} =} inputdlg (@var{PROMPT} [,@var{TITLE} [,@var{ROWSCOLS}, @var{DEFAULTS}]])
19 ## Returns the user's inputs from a multi-textfield dialog box in form of a cell array of strings.
20 ## If the dialog is closed by the Cancel button, an empty cell array is returned.
24 ## The first argument @var{PROMPT} is mandatory.
25 ## It is a cell array with strings labeling each textfield.
27 ## The optional string @var{TITLE} can be used as the caption of the dialog.
29 ## The size of the text fields can be defined by the argument @var{ROWSCOLS},
30 ## which can have three forms:
31 ## - a scalar value which defines the number of rows used for each text field.
32 ## - a vector which defines the individual number of rows used for each text field.
33 ## - a matrix which defines the individual number of rows and columns used for each text field.
35 ## It is possible to place default values into the text fields by supplying
36 ## the a cell array of strings or number for the argument @var{DEFAULTS}.
40 ## @seealso{errordlg, helpdlg, listdlg, questdlg, warndlg}
42 function varargout = inputdlg(prompt,varargin)
44 switch length (varargin)
46 title = "Input Dialog";
48 defaults = cellstr(cell(size(prompt)));
52 defaults = cellstr(cell(size(prompt)));
56 defaults = cellstr(cell(size(prompt)));
60 defaults = varargin{3};
64 % specification of text field sizes as in Matlab
65 % Matlab requires a matrix for lineNo, not a cell array...
66 % rc = [1,10; 2,20; 3,30];
68 % r1 1 10 first text field is 1x10
69 % r2 2 20 second text field is 2x20
70 % r3 3 30 third text field is 3x30
72 % only scalar value in lineTo, copy from lineNo and add defaults
73 rowscols = zeros(size(prompt)(2),2);
76 rowscols(:,1) = lineNo;
77 elseif isvector(lineNo)
78 % only one column in lineTo, copy from vector lineNo and add defaults
79 rowscols = zeros(size(prompt)(2),2);
80 % rows from colum vector lineNo, columns are set to default
82 rowscols(:,1) = lineNo(:);
83 elseif ismatrix(lineNo)
84 if (size(lineNo)(1) == size(prompt)(2)) && (size(lineNo)(2) == 2)
85 % (rows x columns) match, copy array lineNo
90 disp('inputdlg: unknown form of lineNo argument.');
94 % convert numeric values in defaults cell array to strings
95 defs = cellfun(@num2str,defaults,'UniformOutput',false);
96 rc = arrayfun(@num2str,rowscols,'UniformOutput',false);
98 user_inputs = java_invoke ("org.octave.JDialogBox", "inputdlg", prompt, title, rc, defs);
100 if isempty(user_inputs)
101 varargout{1} = {}; % empty
103 varargout{1} = cellstr (user_inputs);