assert(isnumeric(u) && (isvector(u) || ismatrix(u)), 'u must be a numeric vector or matrix.'); assert(isscalar(t) && t >= 0, 't must be a non-negative scalar.'); assert(isa(f, 'function_handle'), 'f must be a function handle.'); assert(isnumeric(n) && isscalar(n) && n > 0 && mod(n,1) == 0, 'n must be a positive integer.'); assert(isa(b, 'Base'), 'b must be an object of class Base or its subclass.'); assert(islogical(flag) && isscalar(flag), 'flag must be a logical scalar (true or false).'); assert(isstruct(params) && all(isfield(params, {'a', 'b', 'c'})), 'parameters must be a struct with fields a, b, and c') % ... end
switch option case'Option1' result = 'You selected Option 1'; case'Option2' result = 'You selected Option 2'; case'Option3' result = 'You selected Option 3'; end end
>> validatestring('G',{'green','red'}) ans = 'green' >> validatestring('bla',{'black','blue'}) ans = 'black'
下面的匹配则会报错
1 2 3 4 5 6 7 8
>> validatestring('u',{'black','blue'}) Expected input to match one of these values: 'black', 'blue' The input, 'u', did not match any of the valid values. >> validatestring('ue',{'black','blue'}) Expected input to match one of these values: 'black', 'blue' The input, 'ue', did not match any of the valid values.
inputParser
inputParser 是输入参数解析器类型,和 C++ 中常见的命令行参数解析器非常类似,只不过在这里是对函数参数进行检查。
假设我们有一个函数 func2,它接受两个必需的输入参数 a 和 b,一个可选的输入参数 c,以及一个键值对参数 Verbose。
help name 会显示 name 指定的功能的帮助文本,例如函数、方法、类、工具箱、变量或命名空间。
help 则会显示与先前操作相关的内容。
例如查找函数的帮助信息
1
help abs
输出内容如下
1 2 3 4 5 6 7 8 9
abs Absolute value. abs(X) is the absolute value of the elements of X. When X is complex, abs(X) is the complex modulus (magnitude) of the elements of X.
See also sign, angle, unwrap, hypot.
Documentation for abs Other uses of abs
对于不同的 name,help 的处理逻辑略有不同,具体来说:
如果 name 是变量,help 显示该变量的类的帮助文本。
要获取某个类的方法/属性的帮助,可以指定类名和方法名称(以句点分隔)。例如,要获取 classname 类的 methodname 方法的帮助,请键入 help classname.methodname。
如果 name 出现在 MATLAB 搜索路径上的多个文件夹中,help 将显示在搜索路径中找到的 name 的第一个实例的帮助文本。
如果 name 指定文件夹的名称或部分路径,help 函数默认将列出文件夹中每个程序文件的帮助文本的第一行。如果文件夹中含有特殊的 Contents.m 文件(通常包括这个文件夹中的内容说明),会影响这里的行为。
legendre - Associated Legendre functions sym.legendreP - Legendre polynomials gauss_legendre - Computation of the Nodes and Weights for the Gauss-Legendre Quadrature. MatLegendre - : A class that provides Legendre polynomial basis functions. MatLegendreDx - : A class that provides Legendre polynomial derivatives. test_MatLegendre - Test suite for MatLegendre class test_MatLegendreDx - Test suite for MatLegendreDx class
functionc = addme(a,b) % ADDME Add two values together. % C = ADDME(A) adds A to itself. % % C = ADDME(A,B) adds A and B together. % % See also SUM, PLUS.
switch nargin case2 c = a + b; case1 c = a + a; otherwise c = 0; end
classdef myClass % myClass Summary of myClass % This is the first line of the description of myClass. % Descriptions can include multiple lines of text. % % myClass Properties: % a - Description of a % b - Description of b % % myClass Methods: % doThis - Description of doThis % doThat - Description of doThat
properties a % First property of myClass
% b - Second property of myClass % The description for b has several % lines of text. b % Other comment end
methods function obj = myClass end
function doThis(obj) % doThis Do this thing % Here is some help text for the doThis method. % % See also DOTHAT.
disp(obj) end function doThat(obj) % doThat Do that thing % Here is some help text for the doThat method. % % See also DOTHIS.
Running rightTriTest .. ================================================================================ Error occurred in rightTriTest/Test3_30_60_90Triangle and it did not run to completion. --------- Error ID: --------- 'MATLAB:assertion:failed' -------------- Error Details: -------------- Error using assert Assertion failed. Error in rightTriTest (line 31) assert(angles(1) == 30) ================================================================================ . ================================================================================ Error occurred in rightTriTest/Test4_SmallAngleApproximation and it did not run to completion. --------- Error ID: --------- '' -------------- Error Details: -------------- Error using assert Problem with small angle approximation Error in rightTriTest (line 39) assert(approx == smallAngle, 'Problem with small angle approximation') ================================================================================ . Done rightTriTest __________
Failure Summary:
Name Failed Incomplete Reason(s) =========================================================================== rightTriTest/Test3_30_60_90Triangle X X Errored. --------------------------------------------------------------------------- rightTriTest/Test4_SmallAngleApproximation X X Errored.
classdef FigurePropertiesTest < matlab.unittest.TestCase properties TestFigure end
methods (TestMethodSetup) functioncreateFigure(testCase) testCase.TestFigure = figure; end end
methods (TestMethodTeardown) functioncloseFigure(testCase) close(testCase.TestFigure) end end
methods (Test) functiondefaultCurrentPoint(testCase) cp = testCase.TestFigure.CurrentPoint; testCase.verifyEqual(cp,[00], ... 'Default current point must be [0 0].') end
functiondefaultCurrentObject(testCase) import matlab.unittest.constraints.IsEmpty co = testCase.TestFigure.CurrentObject; testCase.verifyThat(co,IsEmpty, ... 'Default current object must be empty.') end end end