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.
Related
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?
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."
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);
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 9 years ago.
I was asking myself if there was a quicker way to do this in matlab :
Imagine we have a 10x2 vector V and we want to use the x dimension (number of lines, here 10) in a function or do whatever we want with it. The way I usually do it is this :
[x y]=size(V);
function(x)
But would it be possible to make it differently? Soemething like
function(size(V)(1))
Thanks for your help !
MATLAB's size can take a second input argument, indicating the dimension of which you would like to know the size. The output is scalar in that case:
x = size(V,1);
y = size(V,2);
See help size for more details.
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.