function handle in MATLAB
来源:互联网 发布:淘宝1元秒杀 编辑:程序博客网 时间:2024/05/16 00:36
The function handle operator in MATLAB acts essentially like a pointer to a specific instance of a function. Some of the other answers have discussed a few of its uses, but I'll add another use here that I often have for it: maintaining access to functions that are no longer "in scope".
For example, the following function initializes a value count, and then returns a function handle to a nested function increment:
function fHandle = start_counting(count) disp(count); fHandle = @increment; function increment count = count+1; disp(count); endend
Since the function increment is a nested function, it can only be used within the functionstart_counting (i.e. the workspace of start_counting is its "scope"). However, by returning a handle to the function increment, I can still use it outside of start_counting, and it still retains access to the variables in the workspace of start_counting! That allows me to do this:
>> fh = start_counting(3); %# Initialize count to 3 and return handle 3>> fh(); %# Invoke increment function using its handle 4>> fh(); 5
Notice how we can keep incrementing count even though we are outside of the functionstart_counting. But you can do something even more interesting by calling start_counting again with a different number and storing the function handle in another variable:
>> fh2 = start_counting(-4); -4>> fh2(); -3>> fh2(); -2>> fh(); %# Invoke the first handle to increment 6>> fh2(); %# Invoke the second handle to increment again -1
Notice that these two different counters operate independently. The function handles fh and fh2 point to different instances of the function increment with different workspaces containing unique values forcount.
In addition to the above, using function handles in conjunction with nested functions can also help streamline GUI design, as I illustrate in this other SO post.
- function handle in MATLAB
- 函数句柄 Function Handle MATLAB
- matlab 函数句柄(function handle)
- roots function in MATLAB
- isnan function in matlab
- find function in matlab
- Use the edge function in matlab
- Specify Callbacks in Function Calls matlab
- How to call Matlab function in C++
- fuck the function “reshape” in matlab
- size, length, numel function in matlab
- round() Function in Python and Matlab
- MATLAB 函数句柄Function handle的用法(Af = @(x) A*x;)
- Matlab中函数句柄(function handle)的作用及介绍
- matlab 判断是否handle
- Handle Garbage In
- Handle Error in VBA
- DFS lock handle in RAC
- css知识笔记(四)——代码简写、颜色值、长度值
- 基于流量的协议分析
- ffmpeg常用命令
- Android自动接听&挂断电话(包含怎么应对4.1以上版本的权限检)
- 第10题
- function handle in MATLAB
- 第7周-项目3-分数类中的运算符重载-拓展分数的加减乘除(分数与整型 )
- 第11题
- HTML、XML和XHTML的联系与区别
- JAVAIO流小节
- 【转自mos文章】在linux 下正确分辨用的是哪种cpu
- 友情提醒:欲开发android5.0以上应用,请全部更新开发工具至最新
- 第四章第7题
- c++操作txt