Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档 > MATLAB技术文章
MATLAB技术文章 MATLAB Technical Articles From Mathworks
 
 
主题工具 显示模式
旧 2019-11-24, 15:00   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
默认 Time for an Argument

For those Monty Python fans reading this post, check out the Argument Clinic. Recently both Sean and Jiro, twice! posted about the new arguments capability available for input argument checking. I wanted to show you some more about this great feature! I've also discussed argument checking over the years, including this post.

Contents

What Makes a Program Good, or Useful?

Of course, this in the eye of the user frequently, but certainly it helps if you get informative messages when you misuse the program. These messages should help guide you so you can properly use the program without a major headache. To do so generally requires some amount of error checking on the part of the programmer, thinking about what can go wrong, and making sure to give good information if/when it does.

I'm going to show you some code snippets from a routine. The function in question is modeling the dynamics of a virus, given parameters such as the infection rate.

Original error checking, circa 2007

In this first code, I will show you how we did some important error checking about 12 years ago. In 35 lines, we check to see if the user called the function this code is embedded in with 0, 1, or 5 inputs. And we check the types of the inputs. If not all the inputs are supplied by the user, we set the unsupplied variables with default values.

%  Allowing for variable number of arguments in
switch(nargin)
case 5
Tpoints = varargin{1}; % time to evaluate at
beta = varargin{2}; % infection rate
c = varargin{3}; % clearance rate of virus
delta = varargin{4}; % death rate of infected cells
p = varargin{5}; % viral production rate of infectious cells
case 1
Tpoints = varargin{1}; % time to evaluate at
beta = 3.4e-5; % infection rate
c = 3.3; % clearance rate of virus
delta = 3.4; % death rate of infected cells
p = 7.9e-3; % viral production rate of infectious cells
case 0
% Use all default values
Tpoints = 0:0.1:10; % time to evaluate at
beta = 3.4e-5; % infection rate
c = 3.3; % clearance rate of virus
delta = 3.4; % death rate of infected cells
p = 7.9e-3; % viral production rate of infectious cells
otherwise
error('virusSolve:Input:InvalidNumberOfInputs', ...
'This function expects 0, 1, or 5 inputs.');
end
% Error checking - make sure all inputs are valid
validateattributes(Tpoints, {'numeric'}, {'vector', 'nonnegative'},mfilename,'Tpoints',1);
validateattributes(beta , {'numeric'}, {'scalar', 'real'});
validateattributes(c , {'numeric'}, {'scalar', 'real'});
validateattributes(delta , {'numeric'}, {'scalar', 'real'});
validateattributes(p , {'numeric'}, {'scalar', 'real'});

You can see heavy use of validateattributes as well as the use of nargin, and switch, case, otherwise.

Another solution, similar time frame

There was another solution availabe in about the same time frame using a function called inputParser. Here's what that error checking code looks like.

%  Create input parser
ps = inputParser();
% Define optional arguments with default values and error checking
ps.addOptional('Tpoints', 0:0.1:10, ...
@(x) validateattributes(x, {'numeric'}, {'vector', 'nonnegative'}));
ps.addOptional('beta', 3.4e-5, ...
@(x) validateattributes(x, {'numeric'}, {'scalar', 'real'}));
ps.addOptional('c', 3.3, ...
@(x) validateattributes(x, {'numeric'}, {'scalar', 'real'}));
ps.addOptional('delta', 3.4, ...
@(x) validateattributes(x, {'numeric'}, {'scalar', 'real'}));
ps.addOptional('p', 7.9e-3, ...
@(x) validateattributes(x, {'numeric'}, {'scalar', 'real'}));
% Parse
parse(ps, varargin{:});
% Extract parameters
Tpoints = ps.Results.Tpoints;
beta = ps.Results.beta;
c = ps.Results.c;
delta = ps.Results.delta;
p = ps.Results.p;

A bit better if you count lines, we're down to 25, from 35. In this case, you see that the limitation of number of input arguments is relaxed. It can go from 0 to 5 now.

And the modern way, circa 2019

In MATLAB release 2019b, we introduced a new way to perform input argument validation, using the arguments block at the beginning of the function code. Here's how to do what we've previously accomplished in 35, then 25 lines of code.

   arguments % R2019b or newer
Tpoints(1, :) double {mustBeNumeric, mustBeNonnegative} = 0:0.1:10
beta(1, 1) double {mustBeReal} = 3.4e-5
c(1, 1) double {mustBeReal} = 3.3
delta(1, 1) double {mustBeReal} = 3.4
p(1, 1) double {mustBeReal} = 7.9e-3
end

In 7 lines of code, you can see that we specify the conditions we want for each input, and supply a default value if the input is missing.

This is just a simple example. There is a lot more fodder you can bring to arguments. Check out the documentation to see the possibilities.

Do you have arguments?

How do you resolve your arguments? Let me know here.


Get
the MATLAB code



Published with MATLAB® R2019b



more...
poster 当前离线   回复时引用此帖
 


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 02:56


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.