Conditionals in argument blocks
Show older comments
Is it possible to make the default value of one optional variable depend on the value of another optional variable?
In particular suppose I define a function with required argument shape and optional argument v1 so that when. The variable shape can take one of two names, and the variable v1 has different default values depending on the value of shape, or else can be specificed by a name value pair.
I should be able to make the calls
myFunction('rectangle')
and the arguments block should assign v1=[1 0].
If I make the call
myFunction('triangle')
and the arguments block should assign v1=[1 sqrt(3)]/2
But then I should be able to override the defaults with the calls
myfunction('rectangle',v1=[6 4])
or else
myfunction('rectangle',v1=[5 21])
and opts.v1 will take the value specified
Accepted Answer
More Answers (1)
Chidvi Modala
on 8 Jun 2021
You may refer to the following piece of code
function v1 = myfunction(shape, varargin)
if strcmp(shape,'rectangle')
if nargin >1
v1 = varargin{1};
else
v1=[1 0];
end
elseif strcmp(shape,'triangle')
if nargin >1
v1 = varargin{1};
else
v1=[1 sqrt(3)]/2;
end
end
end
3 Comments
Roy Goodman
on 8 Jun 2021
Chidvi Modala
on 9 Jun 2021
The following link might be of help to you
Roy Goodman
on 9 Jun 2021
Categories
Find more on Simulink Functions in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!