User input
Easy functions in Matlab for getting user input:
input: reading a number from the command line
see help input
inputdlg: pop up a window to get multiple text input items from the user
see help inputdlg
listdlg: pop up a window to allow the user to choose multiple strings from a list
see help listdlg
Reading in a cell list of strings
Sometimes we want to allow the user to input a variable that is not a number or a string, like a cell list of strings. An example of a cell list of strings is:
A = {'mystring1','mystring2','mystring3'}
One option is to allow the user to enter the cell list of strings as a string to be evaluated
myinput = input('Enter a cell list of strings','s');
suppose the user types the following string: {'mystring1','mystring2','mystring3'}
We can create the variable using the eval command:
A = eval(myinput);
We might instead wish to give the user an example string to edit. We can do this using the inputdlg function.
mydefaultanswer = '{''mystring1'',''mystring2'',''mystring3''}';
% the double single quotes indicate that Matlab should interpret
%that we mean that the string should have a quote at that place,
%rather than ending the string; if we only wrote a single quote
%Matlab would take it to mean that the string ended at that point;
%that is how it interprets the final single quote
prompt={'Enter the cell list of strings'};
name = 'Input cell list of strings';
defaultanswer = { mydefaultanswer };
numlines = 1;
options.Resize = 'on';
answer=inputdlg(prompt,name,numlines,defaultanswer,options);
% compare what we did here to the example in help inputdlg
if ~isempty(answer),
A=eval(answer{1});
else,
A = []; % if the user clicked cancel set A to be empty
end;
Sometimes we want the user to select a subset of strings from a list
Suppose we want the user to select a subset from a cell list of strings:
dirlist = {'t00001','t00002','t00003','t00004'};
[s,ok]=listdlg('PromptString','Select from this list',...
'SelectionMode','multiple','ListString',dirlist);
if ok,
A = dirlist(s); % set A equal to the subset of dirlist that the user selected
else,
A = []; % set A to be empty if the user clicked cancel
end;
% compare this to the example in help listdlg