How to define functions with multiple default parameters in Matlab? [duplicate] - matlab

This question already has answers here:
How to deal with name/value pairs of function arguments in MATLAB
(15 answers)
How do I set default values for functions parameters in MATLAB?
(17 answers)
Optional args in MATLAB functions
(4 answers)
Closed 3 years ago.
I am surprised that I couldn't find anything that exactly answers my question about this.
I want to define a function in Matlab where I can either enter a parameter or not.
What I mean is i.e. in Python I can write:
def x(a=0, b=2, c=4):
return
and can call it with either
x(b=2, c=4)
x(1, 2, 3)
x(a=2, c=4)
I already saw the exists functionality but that only works for the last parameter to be optional.
Is there any way to do this in Matlab, where I can put in the parameters as I like?

Related

# in Function definition in Matlab [duplicate]

This question already has answers here:
What is the # operator (at sign) in MATLAB?
(3 answers)
#(t) mean in Matlab? [duplicate]
(2 answers)
Closed 4 years ago.
I was just browsing through a code and I found the following line :
other_function(#(t)(xx(t,g)))
where other_function,xx are already defined functions and g is already defined.
Here is the code for xx
function [val]=xx(x,y)
val=x+y;
end;
SO now I am unable to understand the meaning of #(t)(xx(t,g))
It is a function handle. It is useful to pass functions as parameters. You can find more in the MATLAB documentation
Just an example: suppose you have a simple function
function y = computeSquare(x)
y = x.^2;
end
than you can compute an integral in this way:
q = integral(#computeSquare,0,1);
In your example: other_function declares as an input parameter a function t and another parameter called g.

How to define array as parameter in MatLab? [duplicate]

This question already has answers here:
Is it possible to enforce input argument data types in MATLAB?
(4 answers)
Closed 7 years ago.
I have a function:
function recon_mm(lvl, Threshold, fileToCompress)
I would like to make Threshold an array. I checked the MatLab documentation but could find no clear example how to state that Threshold is an array,
Matlab is not statically typed and does not have syntax for type annotations, but you can check if your parameter is a scalar and return early.
if isscalar(Threshold)
error('Parameter ''Threshold'' must be an array')
return
end
Note that this will also catch 1x1 arrays, e.g. isscalar([1]) == true.

What does ~ mean inside the brackets of a function call in MATLAB? [duplicate]

This question already has answers here:
suppressing output variables in matlab
(2 answers)
Closed 8 years ago.
For example, I have a function
[power, capacity] = function_name(users, distance, radius)
and the function call is
[~, capacity] = function_name(users, distance, 5);
~ just means that you don't want to store the result in any variable.
here is a detailed explanation:
http://www.mathworks.com/matlabcentral/answers/72537-what-does-a-tilde-inside-square-brackets-mean
And the most relevant section: "when you use [~,palette], that means that you just want the second output of your function, and do not care the first one."

Access second output of Matlab procedure directly [duplicate]

This question already has answers here:
How to elegantly ignore some return values of a MATLAB function
(8 answers)
How do I get the second return value from a function without using temporary variables?
(2 answers)
Closed 9 years ago.
I'm not very good at programming and new to matlab, so sorry if I'm not using the right terminology.
If I use the e.g. fminbnd procedure in Matlab first i get the x-value which minimises and then i get the function value. Is there a neat way for me to get just the minimum function value.
To make it clear, for me it seems I have to do:
[x,y] = fminbnd(h,-10,10)
when I only need y. Is there any way for me to not get x?
Use ~ to suppress x output. Only available in later versions of matlab (=> r2009b).
[~, y] = fminbnd(h, -10, 10);

Does MATLAB support named arguments? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Default Arguments in Matlab
How to deal with name/value pairs of function arguments in MATLAB
What if you need to create a function with tones of optional parameters. Is there a way to call that functions later on naming passed arguments for readability:
foo(123, and=456)
The comparable way of doing this in Matlab is to have name/value pairs:
foo(123,'and',456,'something',[1 2 3])
See the answers to this question on how to deal with them.