It is a good practice to add comments that describe the code. It is helpful for others and even for the coder when returned later.
A single line can be commented using the %
symbol or using the shortkey Ctrl+R
.
To uncomment a previously commented line remove the %
symbol or use shortkey Crtl+T
.
While commenting a block of code can be done by adding a %
symbol at the beginning of each line, newer versions of MATLAB (after 2015a) let you use the Block Comment Operator %{ code %}
.
This operator increases the readability of the code. It can be used for both code commenting and function help documentation.
The Block can be folded and unfolded to increase the readability of the code.
As it can be seen the %{
and %}
operators must appear alone on the lines. Do not include any other text on these lines.
function y = myFunction(x)
%{
myFunction Binary Singleton Expansion Function
y = myFunction(x) applies the element-by-element binary operation
specified by the function handle FUNC to arrays A and B, with implicit
expansion enabled.
%}
%% Compute z(x, y) = x.*sin(y) on a grid:
% x = 1:10;
y = x.';
%{
z = zeros(numel(x),numel(y));
for ii=1:numel(x)
for jj=1:numel(y)
z(ii,jj) = x(ii)*sin(y(jj));
end
end
%}
z = bsxfun(@(x, y) x.*sin(y), x, y);
y = y + z;
end