如何在Matlab中创建一个“闭包函数”,如在python和js? [英] How to create an 'closure function' in Matlab as in python and js?

查看:1176
本文介绍了如何在Matlab中创建一个“闭包函数”,如在python和js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景



在Python和JS中,我们有闭包,它返回一个带有一些预定义变量的函数。例如

  def make_printer(msg):
def printer():
print msg
返回打印机



我们在Matlab中有类似的东西吗?



这里我有一个回调函数

  function show(object,eventdata)
 

code> func = @show;
set(gcf,'WindowButtonMotionFcn',func);但是,我想为这个 show



< c $ c>函数。



目前我使用的是全局变量。



关于匿名函数



是的我们确实在Matlab中有匿名函数。但是在我看来,支持60行程序太简单了。

解决方案

,我将解释我对struct的意思。我认为你调用回调函数的方式略有不同,所以这可能不太好工作)。



如果你有一堆不同的回调函数,你想传递给他们的一堆变量,这是恼人,很难有一个大列表的输入参数为每个函数。相反,我做这样的事情:



首先创建一组UI组件,但不要指定他们的回调函数,例如

  radio1_handle = uicontrol(panel1_handle,'Style','radiobutton',... 
'Min',0,'Max',1,'Value ',0,'Units','normalized','Position',[。8 .8 .2 .25]);

一旦你完成了所有的组件,创建一个你将使用的变量的结构

  vars = struct('varName1',var1,'varName2',var2) 

然后更新UI组件以包含回调函数

  set(radio1_handle,'Callback',{@ radio1_callback,vars}); 

现在创建实际功能

  function radio1_callback(object,eventData,vars)

,只是一种比使用多个参数更简单的方法。


Background

In Python and JS we have closures, which returns a function with some of the variables pre-defined. e.g.

def make_printer(msg):
    def printer():
        print msg
    return printer

Do we have similar stuff in Matlab?

Here I have a callback function

function show(object, eventdata)

that is to be set to the callback of my GUI

func = @show;
set(gcf, 'WindowButtonMotionFcn', func);

However, I want to add some additional parameters to this show function.

Currently I'm using global variables to do that. But I think it would be elegant if we have a 'closures function'.

About anonymous function

Yes we do have anonymous function in Matlab. However it seems to me that it is too simple to support 60-line procedures.

解决方案

Since it looks like my comment was helpful, I'll explain what I mean about the struct thing. I think the way you are calling the callback functions is slightly different, so this may not work so well).

If you have a bunch of different callback functions, and a bunch of variables you want to pass to them, it is annoying and difficult to have a big list of input arguments for each function. Instead, I do something like this:

First create a bunch of UI components , but don't specify their callback functions, e.g

radio1_handle=uicontrol(panel1_handle,'Style','radiobutton',...
    'Min',0,'Max',1,'Value',0,'Units','normalized','Position',[.8 .8 .2 .25]);

once you have made all the components, create a struct of variables you will be using

vars=struct('varName1',var1,'varName2',var2);

then update the UI components to include the callback functions

set(radio1_handle,'Callback',{@radio1_callback,vars});

now create the actual functions

function radio1_callback(object,eventData,vars)

So it's nothing fancy, just a potentially neater way than using multiple arguments.

这篇关于如何在Matlab中创建一个“闭包函数”,如在python和js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆