matlab 函数 命名参数,如何处理MATLAB中的函数参数的名称/值对
我喜歡使用結(jié)構(gòu)為我的選擇。這給你一個簡單的方法來存儲選項和一個簡單的方法來定義它們。此外,整個事情變得相當緊湊。
function example(varargin)
%# define defaults at the beginning of the code so that you do not need to
%# scroll way down in case you want to change something or if the help is
%# incomplete
options = struct('firstparameter',1,'secondparameter',magic(3));
%# read the acceptable names
optionNames = fieldnames(options);
%# count arguments
nArgs = length(varargin);
if round(nArgs/2)~=nArgs/2
error('EXAMPLE needs propertyName/propertyValue pairs')
end
for pair = reshape(varargin,2,[]) %# pair is {propName;propValue}
inpName = lower(pair{1}); %# make case insensitive
if any(strcmp(inpName,optionNames))
%# overwrite options. If you want you can test for the right class here
%# Also, if you find out that there is an option you keep getting wrong,
%# you can use "if strcmp(inpName,'problemOption'),testMore,end"-statements
options.(inpName) = pair{2};
else
error('%s is not a recognized parameter name',inpName)
end
end
總結(jié)
以上是生活随笔為你收集整理的matlab 函数 命名参数,如何处理MATLAB中的函数参数的名称/值对的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 空间说说html,空间说说大全(共100
- 下一篇: 模板多个列表级联_如何使用word制作模