matlab代码规范(自用)
在编写代码的时候,越发觉得一个好的代码需要良好的可读性,在github上看到了别人的代码,写得很规整,觉得很有启发,总结一下。
函数:
- 在前面说清楚函数的作用,输入输出的接口含义,例如
function g = func(k)
% func creates a relationship betwween angle and transformation matrices (formed by vector)
%
% EXAMPLE
% g = func(k)
% creates a relationship betwween g and k
%
% INPUT: input parameters
% k (nx1): curve angle
%
% OUTPUT: output parameters
% g (n,16): curve with n 4x4 transformation matrices reshaped into nxn vector (columnwise)
%
% Author: xxx
% Date: 2022/02/16
% Version: 0.1
- 在前面写清楚每个变量的大小和参数类型以及含义
arguments
k(1,:) double %curve angle
end
另外可选参数也可以用arguments很方便的定义:
比如函数中有option选项,
function [fig] = draw_ctcr(g,tube_end,r_tube,options)
在arguments定义option,可以定义默认值:
在后面进行option的判断
调用函数直接调用。
- 大的版块用两个百分号注释,大板块下的小模块用一个百分号注释。
比如这样
-
画图先初始化,figure,axis,hold on放在画图前面。
-
参数的初始化也放在最前面。
暂时想到这些,有其他的再记录吧。