How to pass Optional-Positional arguments to a function in matlab
12 ビュー (過去 30 日間)
古いコメントを表示
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' ?
0 件のコメント
回答 (1 件)
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')
For more information regarding this you can refer to following doc: https://www.mathworks.com/help/matlab/matlab_prog/parse-function-inputs.html?s_tid=answers_rc2-1_p4_MLT#d120e49390
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Install Products についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!