필터 지우기
필터 지우기

How to pass Optional-Positional arguments to a function in matlab

조회 수: 18 (최근 30일)
Rukmangadh Sai Myana
Rukmangadh Sai Myana 2019년 1월 8일
답변: Vishal Chaudhary 2019년 1월 11일
I am trying to understand the usage of positional arguments in MATLAB and I was referring to this page.
Let's say I have a MATLAB function defined as follows:
function printPhoto(filename,varargin)
p = inputParser;
defaultFinish = 'glossy';
validFinishes = {'glossy','matte', 'colorful'};
checkFinish = @(x) any(validatestring(x,validFinishes));
defaultColor = 'RGB';
validColors = {'RGB','CMYK','colorful'};
checkColor = @(x) any(validatestring(x,validColors));
defaultWidth = 6;
defaultHeight = 4;
addRequired(p,'filename',@ischar);
addOptional(p,'finish',defaultFinish,checkFinish);
addOptional(p,'color',defaultColor,checkColor);
addParameter(p,'width',defaultWidth,@isnumeric);
addParameter(p,'height',defaultHeight,@isnumeric);
parse(p,filename,varargin{:});
end
When I call the above function as follows:
printphoto('myFile.img', 'colorful')
Is it possible to make this second argument to correspond to the second optional positional argument in the function definition i.e. color='colorful' and not finish='colorful' ?

답변 (1개)

Vishal Chaudhary
Vishal Chaudhary 2019년 1월 11일
Pass the input as a parameter name and value pair. For example the below should work:
printPhoto('myfile.gif','color','colorful')

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by