how to use varagin with parameter
5 views (last 30 days)
Show older comments
function definedAndVariableNumInputs(X,Y,varargin)
disp("Total number of input arguments: " + nargin)
formatSpec = "Size of varargin cell array: %dx%d";
str = compose(formatSpec,size(varargin));
disp(str)
if varargin{1}=="f"
??
end
varargin{2}=="b"
??
end
acceptVariableNumInputs(3,4,"b")
hi,I want to call the function with additional parameters
If I pass the parameter called "f" I want to pass it the value 3
If I pass the "b" parameter I want to pass the value 5
(it's an example)
Now with varagin I can see the values I pass but not what they refer to... how to do this?
Example I just want to pass it the value of parameter "b" ..
How does the function understand that the only parameter passed refers to "b"?
1 Comment
Voss
on 19 Sep 2023
It's not clear what you are trying to do.
Can you write down a few examples of how this function would be called and how it should interpret the input arguments in each case?
Answers (2)
Matt J
on 19 Sep 2023
Edited: Matt J
on 19 Sep 2023
Another possibility:
out=definedAndVariableNumInputs(10,20,f=3)
function out=definedAndVariableNumInputs(X,Y,varargin)
out.X=X;
out.Y=Y;
out.f=[];
out.b=[];
settings=struct(varargin{:});
for f=string(fieldnames(settings));
out.(f)=settings.(f);
end
end
0 Comments
See Also
Categories
Find more on Search Path in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!