码迷,mamicode.com
首页 > 其他好文 > 详细

Matlab基础学习--------函数句柄

时间:2014-09-10 21:12:21      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:matlab   函数句柄   句柄   

函数句柄是Matlab的一种常见数据类型。

函数句柄创建使用 @ 或者 str2func()来生成函数句柄。

>> %创建一个cos()函数句柄
>> %方法1:handle=@functionname
>> hcos=@cos

hcos = 

    @cos

>> %方法2:fh = str2func('cos')
>> fh = str2func('cos')

fh = 

    @cos

>> functions(hcos) %使用functions对其进行检测

ans = 

    function: 'cos'
        type: 'simple'
        file: ''

>> functions(fh) %使用functions对其进行检测

ans = 

    function: 'cos'
        type: 'simple'
        file: ''

>> isa(hcos,'function_handle')  %使用isa进行检测

ans =

     1

>> isa(fh,'function_handle')  %使用isa进行检测

ans =

     1

>> whos   %查看句柄的所有信息
  Name           Size            Bytes  Class              Attributes
                       
  fh             1x1                16  function_handle              
  hcos           1x1                16  function_handle                                      

函数句柄的基本用法举例:

>> sqrt=@(x,y)(x^y)    %定义函数句柄,第一个括号中为变量,第二个括号为表达式

sqrt = 

    @(x,y)(x^y)

>> a=sqrt(2,5)       %直接调用实现运算

a =

    32


Matlab基础学习--------函数句柄

标签:matlab   函数句柄   句柄   

原文地址:http://blog.csdn.net/z1137730824/article/details/39185191

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!