I would like to use inputParser on the function f(a,b,c,d), where a is mandatory and b-d are optional. I would like to keep b-d named = not use f(a, varargin), because that varargin doesn't tell you anything when you actually want to use the function. The simple p.parse(a,b,c,d) obviously does not work unless all 4 parameters are given.
The only way I know how to do it is to use setDefaultValue(argname, value), and then use p.parse(...) for checks if needed. But is there a simple way to hack this using just inputParser (without tons of ifs)?
As a side question, there is no easy way to do named arguments like in C# in MATLAB, right? So I would have that f(a,b,c,d) and use as such. First argument is a, then b, then c ... But when the user wanted to call it like f(a, 'd', 3, 'c', 2), he could. In this example, he would keep default b and use 3 for d and 2 for c. Without tons of work on my side of course - I know how to do it, writing my own parsing of the thing, but I would like a simpler solution (like other person's parsing :))
Im going to give you an alternative answer: Don't be lazy, write docs.
If you do this:
function [out]= foo(a,b,c,d)
%FOO does foo things
%
% FOO(a,b) does very foo thins to a and b
%
% FOO(a,b,c) is quite foo-y on c also
%
% FOO(a,b,c,d) c and d are optional FOO things
if (nargin==3)
...
then if the user presses F1 or types help foo they will get all the information in the comments.
To have users able to put inputs such as foo(1,2,'d',5) in my code, I use the following "inputparser" for optional parameters:
function [c,d]=parse_inputs(argin)
opts= {'c','d'};
defaults=ones(length(opts),1);
% Check inputs
nVarargs = length(argin);
if mod(nVarargs,2)
error('InvalidInput','Invalid number of inputs')
end
% check if option has been passed as input
for ii=1:2:nVarargs
ind=find(ismember(opts,lower(argin{ii})));
if ~isempty(ind)
defaults(ind)=0;
else
error('InvalidInput',['Optional parameter "' argin{ii} '" does not exist' ]);
end
end
for ii=1:length(opts)
opt=opts{ii};
default=defaults(ii);
% if one option is not default, then extrac value from input
if default==0
ind=double.empty(0,1);jj=1;
while isempty(ind)
ind=find(isequal(opt,lower(argin{jj})));
jj=jj+1;
end
if isempty(ind)
error('InvalidInput',['Optional parameter "' argin{jj} '" does not exist' ]);
end
val=argin{jj};
end
switch opt
case 'c':
if default;
c=4;
else
c=val;
end
...
otherwise
error('InvalidInput',['Invalid input name:', num2str(opt),'\n No such option in SART()']);
end
end
Then The first thing I do inside foo is:
[c,d]=parse_inputs(varargin);
The first part of the question on how to handle varying number of inputs without varargin was answered well by Ander Biguri's answer.
Regarding the second part of the question on how to parse inputs in pairs, here is my simplified "input parser":
function parseVarargin( argin )
% Simplified function to parse varargin property-value pairs
% Read input arguments
if mod(numel(argin),2)
error('Uneven number of inputs')
end
for i = 1 : 2 : numel(argin)-1
if ~ischar(argin{i})
error('Properties must be strings')
end
isValid = evalin('caller', sprintf('exist(''%s'',''var'')', argin{i}));
if isValid && ~strcmp(argin{i},'MRX')
assignin('caller', argin{i}, argin{i+1})
else
error(['Undefined property %s. ', ...
'Please note case-sensitivity.'], argin{i});
end
end
To use it, simply set the default variable values and the call parseVarargin on varargin:
function foo( a, varargin )
%FOO general description
%
% Usage:
% foo(a)
% foo(a, 'property-value-pairs')
%
% Properties:
% b : This ...
% c : That ...
% Default property values
b = 1;
c = [];
% Read property-value paris
parseVarargin( varargin );
% Do other stuff
Related
Thanks in advance,
I want to write a new Matlab function code with different options for sub-algorithms for two distinct steps dealing the input argument/s. An example of one solution can be illustrated by use of switch-case statement such as:
function Return = Calculator(input,option1,option2)
switch option1
case 'Algorithm1_Opt1'
sub=f_Algorithm1_Opt1(input)
case 'Algorithm2_Opt1'
sub=f_Algorithm2_Opt1(input)
end
switch option2
case 'Algorithm1_Opt2'
Return=f_Algorithm1_Opt2(sub)
case 'Algorithm2_Opt2'
Return=f_Algorithm2_Opt2(sub)
end
My question: is there a way to specify custom options structure for an user-defined function as in optimset like;
options = optimset('param1',value1,'param2',value2,...)
such as;
options = optimset('Display','iter','TolX',1e-8)
So at last, I want to call my function as;
Return_exp=Calculator(input_exp,'option1','Algorithm1_Opt1','option2','Algorithm2_Opt2')
Regards.
As others have pointed out you can use the inputParser class or varargin. The inputParser implementation is sometimes too simplistic for coding complex problems. I usually prefer using varargin in these circumstances. Below are some templates I used.
For parameter parsing in functions:
function output=myfunc(requiredArg1,requiredArg2,varargin)
%% Parse Param-Value Inputs
PARAM_NAMES={'name1','name2'};
PARAM_VALUES={value1_default,value2_default};
n=1;
N=numel(varargin);
while n<N
% Read parameter name.
if ischar(varargin{n}) && isrow(varargin{n})
tf=strcmpi(varargin{n},PARAM_NAMES);
if any(tf)
% Set parameter value.
PARAM_VALUES{tf}=varargin{n+1};
else
error('Unrecognized parameter name.');
end
else
error('Unrecognized input. Parameter name must be a string.');
end
n=n+2;
end
if n<=N
warning('An extra input argument was provided, but is ignored.');
end
%% Program
end
For parameter parsing in constructors for handle classes:
methods
function obj=ObjectName(requiredArg1,requiredArg2,varargin)
%% Set Object Properties Using Param-Value Inputs
n=1;
N=numel(varargin);
while n<N
% Read property name.
if ischar(varargin{n}) && isrow(varargin{n})
mp=findprop(obj,varargin{n});
if isempty(mp)
error('''%s'' is not a property of the %s class.',varargin{n},class(obj));
elseif strcmpi(mp.SetAccess,'public')
obj.(varargin{n})=varargin{n+1};
else
error('Denied access to set the ''%s'' property.',varargin{n});
end
n=n+2;
end
end
if n<=N
warning('An extra input argument was provided, but is ignored.');
end
%% Program
end
end
The first template can be also be used for parameter parsing in constructors for value classes by setting PARAM_NAMES=properties(obj).
Is there a way to have one function that can return two different variables, but only one at a time AND knowing which one is returned in the function call?
example:
I have the following function in which only one of the outputs is valid (the other one would be [])
function [a,b] = AlternatingOutput (input)
if input == 1
return ONLY A
else
return ONLY B
end
end
and i call it in a script
[a,b] = AlternatingOutput (input)
i want a way to say the following (pseudocode):
if (function outputs a)
[a,~] = AlternatingOutput(input)
elseif (function outputs b)
[~,b] = AlternatingOutput(input)
end
the script is run in a loop, and later i need the newest Valid values for a and b, so i cannot overwrite one of the two with []
I do understand that I could just write a function that checks which variable will be output, but I was wondering if there is a more elegant way.
I hope I have made my question clear, and I hope someone can answer me :)
There is no way to tell if an output argument is actually used. You may check the number of output arguments using nargout and it would allow to distinguish between [a] = AlternatingOutput(input) and [~,b] = AlternatingOutput(input)
I don't know the full context of your problem, but maybe you can put all your variables into a struct? Simply pass this struct everytime you call the function and let it decide which variables to manipulate. (This might be slow in some programming languages, but not in matlab).
How about retuning a cell?
function [ ab ] = testfun( input )
if input
ab={'ax'};
else
ab={2};
end
end
No worries about what is in the cell.
thb you could return what ever you want, Matlab does not check the type anyways
If only one of the outputs from the function AlternatingOutput is valid, then you only need to return one output:
function [X] = AlternatingOutput(input)
if input == 1
X = A;
else
X = B;
end
end
To allocate the retured value to either a or b in the loop, put them into a cell:
C = {AlternatingOutput(1), AlternatingOutput(2)};
and then use input to determine which value is change. If input is either 1 or 2 you can just do
for counter = ...
input = mod(input,2)+1;
C{input}=AlternatingOutput(input);
end
If your function doesn't mind accepting more input variables, why not pass a and b as input:
function [a,b] = AlternatingOutput(a,b,input)
if input == 1
a = new_value_for_a;
% b retains its former value
else
% a retains its former value
b = new_value_for_b;
end
end
Then it can be easily called from your script in a loop:
for i= ...
[a,b] = AlternatingOutput(a,b,input);
...
...
end
Sometimes it is desired to make several calls in one command. A simple example could be strrep. Assume you want to replace all parentheses with brackets, all commas with dots and then remove all double quotations. The following pseudo code could then be desired:
strrep(myString, '()', '[]', ',', '.', '"', '')
Is there any way to accomplish this? You could of course go with:
strrep(strrep(strrep(myString, '()', '[]'), ',', '.'), '"', '')
Or save the strings in a cell array and use this in a for loop, but both solutions are incredibly ugly.
The most desired answer, is one that is generic for all functions that work in a similar way.
To directly answer your question, there is really no consistent way of doing this, no. It really depends on the function. If you search the documentation you will often find a way to do this. With strings, at least, you can usually pass cell arrays in place of strings to perform operations on multiple strings, and in this case multiple operations on the same string.
A Solution for This Particular Example
You can easily use regexprep to do this for you. You can pass a cell array of the expressions to match with a corresponding cell array of the replacement values.
regexprep('abc', {'a', 'b', 'c'}, {'1', '2', '3'});
%// '123'
For your specific example, you would do something like:
regexprep(myString, {'\(\)', ',', '"'}, {'[]', '.', ''})
And as an example:
myString = 'This, is a () "string"';
regexprep(myString, {'\(\)', ',', '"'}, {'[]', '.', ''})
%// 'This. is a [] string'
If you don't want to worry about escaping all of the expressions to be regex-compatible, you can use regexptranslate to do that for you.
expressions = regexptranslate('escape', {'()', ',', '"'});
regexprep(myString, expressions, {'[]', '.', ''});
Say you want function foo to work like this:
foo(Variable,Parameter1,Value1);
foo(Variable,Parameter1_1,Value1,Parameter2,Value2,...);
then using recursion:
function[Variable]=FooBar(Variable,varargin)
N=nargin-1; %\\ Count the input parameters
if N>=2
Parameter=varargin{1};
Value=varargin{2};
% Process the first Parameter-value pair
Variable=FooBar(Variable,varargin{3:N}); %\\ Cut first Parameter-Value pair off and pass the rest to foo again
end
This approach allows you to use chain of single parameters, pairs, triplets, quadruplets, etc.
In this perticullar example the pairs are executed as LIFO stack and last unpaired Parameter is ignored. You can also add some conditions to implement foo(IN,Parameter1,Value1,Modifier,Parameter2,Value2,...) and many other properties...
For your perticullar example:
function[MyString]=FooBar(MyString,varargin)
N=nargin-1; %\\ Count the input parameters
if N>=2
Parameter=varargin{1};
Value=varargin{2};
MyString=regexprep(MyString,Parameter,Value)
MyString=FooBar(MyString,varargin{3:N});%\\ Cut first Parameter-Value pair off and pass the rest to foo again
end
Examples:
>> myString='This, is a () "string"';
FooBar(myString,'()','[]','"','',',','.')
ans = This. is a [] string
>> myString='This, is a ("string")';
FooBar(myString,'()','[]','"','',',','.')
ans = This. is a (string)
>> myString='This, is a ("string")';
FooBar(myString,'(','[',')',']','"','',',','.')
ans = This. is a [string]
As already said by #Suever your example can be solved by regexprep and #thewaywewalk has hinted that there is no "general" soluction for all function calls.
Note I do not advocate this as a good way to code -> but its a quirky question and thus here is a suitable quirky solution....
There is lots of reason why you shouldn't do this - namely a nightmare to debug but you could in theory do this with an "intelligent" self calling function...
% Create your own function which takes the following inputs:
% fHandle - function handle to the function of choice
% property - your starting variable
% varargin - a cell array (or single var) of variables to
% pass into the fHandle on each call
% see examples below...
function output = multipleCalls ( fHandle, property, varargin )
% call your primary function using feval and your inputs
% with the 1st group of inputs from the 1st varargin
if iscell ( varargin{1} )
output = feval ( fHandle, property, varargin{1}{:} );
else
output = feval ( fHandle, property, varargin{1} );
end
% remove the varargin variable which has just been used.
varargin(1) = [];
% are they aremore multiple call?
if ~isempty ( varargin )
% if so self call to apply the subsequent calls.
output = multipleCalls ( fHandle, output, varargin{:} );
end
end
% modifying your example to use this method:
multipleCalls( #strrep, 'This, is a () "string"', { '()', '[]' }, { ',', '.' }, { '"', '' } )
% Its probably a longer command and is it any clearer -> probably not...
% Here is another example:
% Create a silly anonymous function
sillyFunction = #(a,b) a + b
% Then you can use it in the same way:
% Where 0 is what you start with and then
% each time you want to add 1, then 2, then 3 and finally 4
multipleCalls ( sillyFunction, 0, 1, 2, 3, 4 )
Function:
My MATLAB function has one output and several input arguments, most of which are optional, i.e.:
output=MyFunction(arg1,arg2,opt1,opt2,...,optN)
What I want to do:
I'd like to give only arg1, arg2 and the last optional input argument optN to the function. I used the tilde operator as follows:
output=MyFunction(str1,str2,~,~,...,true)
Undesired result:
That gives the following error message:
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
The error points to the comma after the first tilde, but I don't know what to make of it to be honest.
Problem identification:
I use MATLAB 2013b, which supports the tilde operator.
According to MATLAB's documentation the above function call should work:
You can ignore any number of function inputs, in any position in the argument list. Separate consecutive tildes with a comma...
I guess there are a few workarounds, such as using '' or [] as inputs, but I'd really like to understand how to correctly use '~' because actually leaving inputs out allows me to use exist() when checking the input arguments of a function.
If you need any further info from me, please let me know.
Thank you very much!
The tilde is only for function declaration. Matlab's mlint recommends to replace unused arguments by ~. The result is a function declared like this function output = MyFunction(a, b, ~, c). This is a very bad practice.
Since you have a function where the parameters are optional, you must call the function with empty arguments output=MyFunction(str1,str2,[],[],...,true).
A better way to do it is to declare the function with the varargin argument and prepare your function for the different inputs:
function output = MyFunction(varargin)
if nargin == 1
% Do something for 1 input
elseif nargin == 2
% Do something for 3 inputs
elseif nargin == 3
% Do something for 3 inputs
else
error('incorrect number of input arguments')
end
It is even possible to declare your function as follows:
function output = MyFunction(arg1, arg2, varargin)
The declaration above will tell Matlab that you are expecting at least two parameters.
See the documentation of nargin here.
... and the documentation of varargin here
To have variable number of inputs, use varargin. Use it together with nargin.
Example:
function varlist2(X,Y,varargin)
fprintf('Total number of inputs = %d\n',nargin);
nVarargs = length(varargin);
fprintf('Inputs in varargin(%d):\n',nVarargs)
for k = 1:nVarargs
fprintf(' %d\n', varargin{k})
end
I am trying to write a minimal function that can be called with a variable number of arguments but that will not throw a wrong number of arguments error if miscalled.
Here is where I start from :
function varargout=fname(varargin)
% FNAME
% Usage: output=fname(input)
% Arguments check
if(nargin~=1 || nargout~=1)
disp('Function fname requires one input argument');
disp('and one output argument');
disp('Try `help fname`');
varargout(1:nargout)={0};
return;
end
input=varargin{1};
output=input;
varargout(1)={output};
end
However this does not work as I would like it to. Is there a way to write a function that :
never throw a "wrong number of arguments" error (so that the rest of the execution can continue)
accepts variable number of input and output arguments and checks them inside the function
(maybe more tricky) if the number of input / output arguments is not correct, does not replace the value of the provided output arguments (so that any misplaced call does not erase the previous value of the output argument)
I am open to any suggestions / other methods.
Thank you for your help.
UPDATE: thanks to #Amro for his answer, I guess what I miss here is either a call by address of reference for Matlab functions or a way to interrupt a function without returning anything and without stopping the rest of the execution.
Here is one way to implement your function:
function varargout = fname(input,varargin)
%# FNAME
%# Usage: output=fname(input)
%%# INPUT
if nargin<1
varargout(1:nargout) = {[]};
warning('Not enough input arguments.'), return
end
if ~isempty(varargin)
warning('Too many input arguments.')
end
%%# YOUR CODE: manipulate input, and compute output
output = input;
%%# OUTPUT
varargout{1} = output;
if nargout>1
warning('Too many output arguments.')
varargout(2:nargout) = {[]};
end
end
Obviously you can customize the warning messages to your liking...
Also, if you want your function to simply print the message instead of issuing warnings, replace all WARNING calls with simple DISP function calls.
Examples of function call:
fname()
fname(1)
fname(1,2)
x = fname()
x = fname(1)
x = fname(1,2)
[x,y] = fname()
[x,y] = fname(1)
[x,y] = fname(1,2)
The above calls execute as expected (showing warning messages when applicable). One caveat though, in the last three calls, if the variable y already existed in the workspace prior to the calls, it would be overwritten by the empty value y=[] in each...
If I understand your question correctly, then the answer is no. If a caller calls a function like this:
[a, b, c] = fname('foo');
then fname is required to return (at least) three outputs. There's no way to tell MATLAB that it should leave b and c alone if fname only returns one output.