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


转自易百教程,以及手头上的一些下载下来的tutorial


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 主函数和子函数

我们写一个名为quadratic 函数计算一元二次方程的根。

则该函数将需要三个输入端,二次系数,线性合作高效的和常数项。它会返回根。 

函数文件quadratic.m将包含的主要quadratic 函数和子函数disc 来计算判别。


function [x1,x2] = quadratic(a,b,c)

%this function returns the roots of

% a quadratic equation.

% 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

% end of quadratic


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;
breadth=4;
depth=3;
% call the function cuboid
volume = cuboid(length,breadth,depth);
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


让我们重写quadratic函数,从前面的例子,但是,这一次的 disc 函数 将是一个嵌套函数。 

创建一个函数文件quadratic2.m,并输入下面的代码:

function [x1,x2] = quadratic2(a,b,c)

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

% end of function quadratic2



12 私有函数

一个私有函数是一个主要的函数,是只看得见一组有限的其它函数。如果不想公开的执行的一个函数,可以创建私有函数。

私有函数驻留特殊的名字私人的子文件夹中。

他们是可见的,只有在父文件夹的函数。


我们重写quadratic 函数。然而,这时候,计算​​的判别式disc 函数,这是一个私有函数。

创建一个子文件夹命名为私人工作目录。它存储在以下函数文件disc.m:


function dis = disc(a,b,c)

%function calculates the discriminant

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

end

% end of sub-function


在工作目录,并创建一个函数quadratic3.m中输入下面的代码:


function [x1,x2] = quadratic3(a,b,c)

%this function returns the roots of

% a quadratic equation.

% 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

% end of quadratic3


12 全局变量 global

全局变量可以共享由一个以上的函数。对于这一点,需要将变量声明为全局在所有的函数可使用。

如果想访问该变量从基工作区,然后在命令行声明的变量。

全局声明必须出现在变量中实际上是使用功能。这是一个很好的做法是使用大写字母为全局变量的名称,以区别于其他变量。


例如在下面的一个例子中,可以看到命令global 后面的 Endtime这一个变量,是作为全局变量,可用于主程序和子函数


% 主程序
nowt = num2cell(fix(clock));
addt = cell(1,6);
addt = {0,0,0,0,0,0 };
addt{1,6} = input('\n\n count down from____sec\n\n'); %% 通过input你想要倒数的秒数,即可以倒数
a = cell2mat(nowt);
b = cell2mat(addt);
count = a + b; %% 这里的count最后会用于下面的倒数,是double格式
global Endtime
Endtime=[count];
t = timer('StartDelay', 0,'Period',0.05,'TasksToExecute', 10000,...
          '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



  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值