0

I have a Matlab function that has numerous name-value parameter inputs. For some of the parameter names, there are a lot of possible values (which are not always obvious) that the user can choose from. What I would like to do is, IF the user calls the name, but does NOT give a value, THEN Matlab would display possible entries AND THEN take the user's input.

For example I have a function such as:

function getSomeData( varargin )

p=inputParser;
defaultData='abc';

addParameter(p, 'Data', defaultData);

parse(p,varargin{:});

end

If the user were to call the function in the command window such as:

>> getSomeData('Data')

in which the user did not give a value for 'Data', the window would display and prompt

>> getSomeData('Data')
No value for 'Data' Given
Possible Values of 'Data' are:
'abc'
'def'
'other'

Please input your 'Data':

in which I could use the result=input(prompt) function.

Any help or advice is very much appreciated! Cheers

RickyG
  • 1
  • 1
  • I'm having a hard time following your code. So you say you have a function already available that takes in name-value pairs? How are these name-values obtained to begin with? Your `getSomeData` function seems to just set up some default data, then goes through a function called `parse`. What does `parse` do? Can you show us **all** of your code? – rayryeng Oct 18 '14 at 05:02

1 Answers1

0

May I ask you to specify the complexity of your input stuff. Either you come from java and think that you need to create an I/O object to be able to read inputs, or else your problem is more complex that the description gives the impression of.

Otherwise, I would give you the design of a less complicated way forward here. One way to do this is to use the nargin property, which finds the number of inputs to the function. Together with nargin, use an if statement (or switch-case?).

if nargin==0
    % print alternative inputs with disp or fprintf.
    % This alternative can also be replaced with comments (single block with
    % no empty rows) right below the function. This will then be seen with
    % the `help funName` command

elseif nargin==1
    % Print description + permitted values. This can be done from a
    % switch-case statement (if you want the switch-case statement
    % can be placed in an external function).

elseif ~mod(nargin,2)
    %parse input pairs and do the calculations.

else
    error('wrong number of input arguments');
    % or
    % fprintf('wrong number of input arguments\n');
    % set outputs to '', {}, [], ...
    % return;
end

I hope this helps even though it is not exactly the solution proposed by you. This is however a simple solution with the advantage that you do not mix up the information parts and execution parts. My guess is that this is a convenient way to work with I/O without implementing a complicated parser.

These problems are otherwise normally solved by a complicated parser with a lot of different commands (compare with the terminal (unix based) of cmd prompt (windows)).

patrik
  • 4,506
  • 6
  • 24
  • 48