필터 지우기
필터 지우기

how to use varagin with parameter

조회 수: 1 (최근 30일)
piero
piero 2023년 9월 18일
편집: Matt J 2023년 9월 19일
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
Voss
Voss 2023년 9월 19일
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?

댓글을 달려면 로그인하십시오.

답변 (2개)

Matt J
Matt J 2023년 9월 19일
편집: Matt J 2023년 9월 19일
Perhaps this is what you're looking for:
opts=myfunc(f=3)
opts = struct with fields:
f: 3 b: []
opts=myfunc(b=5)
opts = struct with fields:
f: [] b: 5
opts=myfunc(f=3,b=5)
opts = struct with fields:
f: 3 b: 5
function opts=myfunc(opts)
arguments
opts.f=[];
opts.b=[];
end
end

Matt J
Matt J 2023년 9월 19일
편집: Matt J 2023년 9월 19일
Another possibility:
out=definedAndVariableNumInputs(10,20,f=3)
out = struct with fields:
X: 10 Y: 20 f: 3 b: []
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

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by