# MATLAB 模块化编程¶

## inputParser¶

R2021a 中正式推出了键值对调用。在此之前，学 MATLAB 的同学大多数是这么搞的

printPhoto('myfile.jpg','height',10,'width',8)

printPhoto('myfile.jpg',height=10,width=8)

Very Pythonic。

## Function Signature¶

https://ww2.mathworks.cn/help/matlab/matlab_prog/customize-code-suggestions-and-completions.html

## Function Argument Validation¶

Introduced in: R2019b

function ret = example( inputDir, proj, options )
%EXAMPLE An example.
% Do it like this.
% See THEOTHEREXAMPLE.
% See https://stackoverflow.com/a/60178631/20148196
arguments
inputDir (1, :) char
proj (1, 1) projector
options.foo char {mustBeMember(options.foo, {'bar' 'baz'})} = 'bar'
options.Angle (1, 1) {double, integer} = 45
options.Plot (1, 1) logical = false
end

% Code always follows 'arguments' block.
ret = [];
switch options.foo
case 'bar'
ret = sind(options.Angle);
case 'baz'
ret = cosd(options.Angle);
end

if options.Plot
plot(proj.x, proj.y)
end

end

## 面向对象¶

• robot.teach()
• teach(robot)

（实例来自 Robotic Toolbox1

## 为啥要折腾这么多¶

MATLAB 一直被语法问题和工程性不足而饱受诟病2，但最近的更新，尤其是近三年的更新，让我看 到了这门语言的发展希望。

## 缺点¶

MATLAB 尚未计划推出类似于 Python Type Hint3，语法级别的支持恐怕是要再等等，不过现在相比以前已经很令人满意了~

1. Robotics Toolbox, Peter Corke. https://petercorke.com/toolboxes/robotics-toolbox/

2. MATLAB is a terrible programming language, Nikolaus Rath‘s Website. https://www.rath.org/matlab-is-a-terrible-programming-language.html

3. PEP 484 -- Type Hints, Guido van Rossum et al. https://peps.python.org/pep-0484/