Does MATLAB support named arguments? [duplicate] - matlab

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.

Related

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

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?

MATLAB: how to reuse plot options? [duplicate]

This question already has answers here:
Is that possible to assemble several options and pass to the plot function in matlab
(4 answers)
Closed 7 years ago.
I have the following plot:
patch('Vertices',rocket_point_cloud,'Faces',rocket_faces,...
'FaceColor','red','EdgeColor','none',...
'BackFaceLighting','reverselit',...
'SpecularStrength',1,'DiffuseStrength',1)
I would like to reuse the plot options, i.e. reuse:
'FaceColor','red','EdgeColor','none',...
'BackFaceLighting','reverselit',...
'SpecularStrength',1,'DiffuseStrength',1
Is it somehow possible to store the above in a variable, e.g. my_options and later on do:
patch('Vertices',other_cloud,'Faces',other_faces,my_options)
Thanks for your help!
Sure. Just define your options in a cell array,
my_options = {'FaceColor','red','EdgeColor','none',...
'BackFaceLighting','reverselit',...
'SpecularStrength',1,'DiffuseStrength',1};
and then expand that cell array into a comma-separated list via curly-brace indexing:
patch('Vertices', rocket_point_cloud, 'Faces', rocket_faces, my_options{:})

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 are some good ways to emulate cascading indexing in MATLAB? [duplicate]

This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Closed 8 years ago.
E.g. I would like to do things such as:
A=4:20;
find(A>5)(2) % want to access the 2nd element of the index array returned by find
Yes, this comes up fairly frequently in different contexts, and the one-line answer is subsref. For your case, it is this:
subsref(find(A>5),struct('type','()','subs',{{2}}))
A much cleaner solution uses an undocumented builtin:
builtin('_paren',find(A>5),2)
As an alternative to ugly syntax or undocumented functionality, you could define a small function like the following,
function outarray = nextind(inarray,inds)
outarray = inarray(inds);
Or an inline function:
nextind = #(v,ii) v(ii);
And call it like nextind(find(A>5),2). This is cleaner than subsref and good if you are doing linear indexing (not subscripts).

Function inside script in Matlab? [duplicate]

This question already has answers here:
In MATLAB, can I have a script and a function definition in the same file?
(7 answers)
Closed 9 years ago.
Can I have fast and short helper local function to use inside script?
Currently I have "FUNCTION keyword use is invalid here" message.
Why?
This is correct, MATLAB does not allow you to define full functions in a script. However, there are at least two solutions that may help you:
You could turn the script into a function. Workspace variables you are referring to from within your scripts would become arguments of the function, and you could return certain result variables.
If your helper function is really small, you may be able to define it in your script as an anonymous functions without the function keyword, as in poly = #(x) x.^2 + 3 * x - 4; - a polynomial function, for example.