# [初学笔记] matlab 中的 function, return,global

1 function的命名不能和matlab中的已有命令函数一致

2 每个function都会保存在一个m文件，并且m文件的名称和function的名称一致

3 function里面用到的函数都是固定单一的

4 function内部运行，如果一定要输出，那就只能用disp

5 function不一定有input和output的，而且input和output的个数可以不限制

6 函数语句一般表达式

funct ion out = MyFunction ( in )
% This i s a comment ( notice the %)
% Explain what your funct ion does here and i t w i l l pop up when
% you type help MyFunction in the con s o l e .
%
% MyFunction returns the square of the input

out = in ˆ 2;

end

7 主函数和子函数

%this function returns the roots of

% It takes 3 input arguments

% which are the co-efficients of x2, x and the

%constant term

% It returns the roots

d = disc(a,b,c); x1 = (-b + d) / (2*a);x2 = (-b - d) / (2*a);

end

function dis = disc(a,b,c)

%function calculates the discriminant

dis = sqrt(b^2 - 4*a*c);

end

% end of sub-function

8  没有input和output的function

% script file for cuboid
length=5;
depth=3;
% call the function cuboid
disp(volume);

function vol = cuboid(len,br,dep)
% Function to return the volume of a cuboid given
% the length len, breadth br and depth dep
vol = len*br*dep;
return;

9 多个input和多个output

function vol = cuboid(len,br,dep)
% Function to return the volume of a cuboid given
% the length len, breadth br and depth dep
vol = len*br*dep;
return;

function [vol, surf] = cuboid(len,br,dep)
% Function to return the volume and surface area of a cuboid given
% the length len, breadth br and depth dep
vol = len*br*dep;
surf = 2*(len*br + len*dep + br*dep);
return;

10 匿名函数的使用

f = @(arglist)expression

power = @(x, n) x.^n;

result1 = power(7, 3)

result2 = power(49, 0.5)

result3 = power(10, -10)

result4 = power (4.5, 1.5)

11 嵌套函数

function x = A(p1, p2)...B(p2)

function y = B(p3)

...

end

...

end

function disc

% nested function

d = sqrt(b^2 - 4*a*c);

end

% end of function disc

disc;x1 = (-b + d) / (2*a);x2 = (-b - d) / (2*a);

end

12 私有函数

function dis = disc(a,b,c)

%function calculates the discriminant

dis = sqrt(b^2 - 4*a*c);

end

% end of sub-function

%this function returns the roots of

% It takes 3 input arguments

% which are the co-efficients of x2, x and the

%constant term

% It returns the roots

d = disc(a,b,c);

x1 = (-b + d) / (2*a);

x2 = (-b - d) / (2*a);

end

12 全局变量 global

% 主程序
nowt = num2cell(fix(clock));
addt{1,6} = input('\n\n count down from____sec\n\n'); %% 通过input你想要倒数的秒数，即可以倒数
a = cell2mat(nowt);
count = a + b; %% 这里的count最后会用于下面的倒数，是double格式
global Endtime
Endtime=[count];
'ExecutionMode','fixedRate');
t.TimerFcn = {@mycallback}; %% 引入function 子函数
start(t)

%子函数
function mycallback(t,events,arg_str)
%mycallback.m
global Endtime
Lefttime=etime(Endtime,clock);
if Lefttime<0
stop(t);
disp('Times up!!')
return
end
LeftMillionSeconds=round(rem(Lefttime,1)*1000);
LeftSeconds=floor(rem(Lefttime,60));
LeftMinutes=floor(rem(Lefttime/60,60));
% r=sprintf('剩余时间:%d分%d秒%d',LeftMinutes,LeftSeconds,LeftMillionSeconds);
% disp(r)

13  return 语句

The return statement indicates the end of the function and asks MATLAB to return to the place from which
the function was called. You can call functions from the command window of course, or from script files, or
even from other functions

11-12 11万+

11-03 737
09-14 8734
04-09 1594
02-13 3万+
04-22 2万+
02-14 6万+
01-06 8896