Process named Arguments

버전 1.5.0.0 (1.9 KB) 작성자: Alan Robinson
Easily add optional, named arguments to your functions, like this: f('var_name', value).
다운로드 수: 610
업데이트 날짜: 2014/1/7

라이선스 보기

Assigns named arguments passed to your function to local variables (as in plot(), etc.). The name of the argument is the name of the local variable in your function.

Example: inputfun('prompt', '>', 'defval', 5.1, 'timeout', 5);

This is the same idea as Matlab's inputParser, but with a focus on being really simple and easy to use. Other implementations of this idea on File Exchange require a lot of lines of code to set this up, where as my code just requires one extra function call near the top of your function.

Usage:
(1) in your function, define local variables with their default values;
(2) call procArgs(varargin)

Minimal error checking: requires that all passed variable names match already defined variables in your function. It would be pretty easy to add type checking if you wanted it (I don't, particularly).

Example:


function test(varargin)
name = 'John';
age = 32;
procArgs(varargin)
fprintf('name=%s, age=%i\n', name, age);

%% example calls
test('name', 'Amber'); % prints Amber, 32
test('age', 99); % prints John, 99
test('Ages', 0); % error, will report that Ages is not defined.

인용 양식

Alan Robinson (2024). Process named Arguments (https://www.mathworks.com/matlabcentral/fileexchange/25881-process-named-arguments), MATLAB Central File Exchange. 검색됨 .

MATLAB 릴리스 호환 정보
개발 환경: R2007a
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Argument Definitions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!
버전 게시됨 릴리스 정보
1.5.0.0

Improved help text; code is identical.

1.3.0.0

better handling of different variable types.

1.2.0.0

Exist only checks against variable names, which is safer and faster.

1.1.0.0

Improved title/description.

1.0.0.0