首页 \ 问答 \ Matlab匿名回调函数参数(Matlab anonymous callback function arguments)

Matlab匿名回调函数参数(Matlab anonymous callback function arguments)

按照Matlab中的模型视图控制器GUI示例,我有一个关于匿名函数回调输入参数的问题

这是一个视图函数,它创建gui句柄并将它们作为输入参数传递给onChanged回调函数。

function handles = View_TimeDomain(m)
    %VIEW  a GUI representation of the signal model

    % build the GUI
    handles = initGUI();
    onChangedF(handles, m);    % populate with initial values

    % observe on model changes and update view accordingly
    % (tie listener to model object lifecycle)
    addlistener(m, 'f', 'PostSet', ...
        @(o,e) onChangedF(handles,e.AffectedObject));
end

我不太明白的第一件事是,根据Matlab文档,第一个参数必须是事件的来源,第二个文件必须是事件数据( Matlab doc1Matlab doc2 ),但在这种情况下它是handles 。 触发事件时,将按预期调用以下onChangedF函数。

function onChangedF(handles, model)
    % respond to model changes by updating view
    if ~ishghandle(handles.fig), return, end
    set(handles.line, 'XData',model.t, 'YData',model.data)
    set(handles.slider, 'Value',model.f);
end

但是,在这种情况下,handle是包含使用initGui()定义的句柄的结构,而不是事件源。

我想这源于匿名函数的定义:

@(o,e) onChangedF(handles, e.AffectedObject)

我是否正确,假设oonChangedF函数输入中未使用的源。 有人可以解释为什么匿名函数的这种语法有效吗? 我认为o需要成为这个特定回调函数的参数。 像这样的东西:

@(o,e) onChangedF(o, handles, e.AffectedObject)

其他参数附加(在函数的末尾)。 然后使用〜来避免这个未使用的参数:

function onChangedF(~, handles, model)
    % respond to model changes by updating view
    if ~ishghandle(handles.fig), return, end
    set(handles.line, 'XData',model.t, 'YData',model.data)
    set(handles.slider, 'Value',model.f);
end

Following this example of a Model View Controller GUI in Matlab, I have a question regarding the anonymous function callback input arguments

Here is the a view function that creates the gui handles and passes them as an input argument to the onChanged callback function.

function handles = View_TimeDomain(m)
    %VIEW  a GUI representation of the signal model

    % build the GUI
    handles = initGUI();
    onChangedF(handles, m);    % populate with initial values

    % observe on model changes and update view accordingly
    % (tie listener to model object lifecycle)
    addlistener(m, 'f', 'PostSet', ...
        @(o,e) onChangedF(handles,e.AffectedObject));
end

The first thing that I don't quite understand is that according to the Matlab documentation the first argument must be the source of the event and the second arument must be the event data (Matlab doc1, Matlab doc2) but in this case it is handles. When the event is triggered, the following onChangedF function is called as expected.

function onChangedF(handles, model)
    % respond to model changes by updating view
    if ~ishghandle(handles.fig), return, end
    set(handles.line, 'XData',model.t, 'YData',model.data)
    set(handles.slider, 'Value',model.f);
end

However, in this case handles is the struct containing the handles defined using initGui() and not the event source.

I guess this stems from the definition of the anonymous function:

@(o,e) onChangedF(handles, e.AffectedObject)

Am I correct, assuming that o is the source which is not used in the onChangedF function input. Can someone explain why this syntax of an anonymous function is valid? I thought that o needs to be an argument of this specific callback function as well. Something like this:

@(o,e) onChangedF(o, handles, e.AffectedObject)

where additional arguments are appended (at the end) of the function. And then just avoid this unused argument using ~:

function onChangedF(~, handles, model)
    % respond to model changes by updating view
    if ~ishghandle(handles.fig), return, end
    set(handles.line, 'XData',model.t, 'YData',model.data)
    set(handles.slider, 'Value',model.f);
end

原文:https://stackoverflow.com/questions/45299016
更新时间:2022-12-03 17:12

最满意答案

你的问题有点模糊,但我会尽力帮你。

如果我遇到这种情况,我会使用api(公共)和秘密(私人)密钥为每个客户端签名。 每个请求都必须包含公钥(作为字段,或标题,或者你喜欢),以便识别客户端,签名标题,以及从a生成的签名(这可能有点过时) ,自从我这样做了一段时间后)HMAC字符串基于发送信息的base64编码和特定客户端的密钥。 由于只有您的服务和客户端应具有此密钥,因此您将能够验证签名是否与客户端不匹配。

包括客户端在其被泄露的情况下重置密钥的方式也是一个好主意。

此时您已经识别并验证了客户端,因此您可以简单地将请求记录在您正在使用的任何类型的数据库中。 如果要验证客户端是否允许发出该请求,则必须构建某种权限系统(可能包含允许每个客户端访问的数据库或允许哪些客户端访问每个资源的数据库表),如以及检查以确保客户端没有超过您设置的任何速率限制。

如果这不是你要求的我道歉,如果你有任何进一步的问题随时问。


You're question is a little vague, but I'll do my best to give you a hand.

If I were in this situation I would utilize api (public) and secret (private) keys for each client, with a signature. Each request would have to include the public key (either as a field, or header, or however you like) in order to identify the client, and a signature header, as well as a signature generated from a (this may be a little off, its been a while since I did this) HMAC string based on the base64 encoding of the sent information and the specific client's secret key. Because only your service and the client should have this key, you will be able to verify if the signature does not match the client.

Including a way for the client to reset the secret key in the event that it is compromised is also a good idea.

At this point you've identified and verified the client, so you can simply record the request in whatever kind of db you're using. If you want to verify that the client is allowed to make that request you will have to build some kind of permission system (perhaps db tables consisting of what each client is allowed to access, or which clients are allowed to access each resource), as well as a check to ensure that the client hasn't gone over whatever rate-limit you've set.

If this isn't quite what you were asking I apologize, and if you have any further questions feel free to ask.

相关问答

更多
  • 查找AOP( 面向方面编程 )这将允许您在方法周围声明拦截器,然后您可以简单地使用拦截器来写入日志文件。 以下是通过反射执行代码的示例。 import java.lang.reflect.Method; public class RunMthdRef { public int add(int a, int b) { return a+b; } public int sub(int a, int b) { return a-b; } public int mul(int ...
  • 我想我刚刚找到了答案 - 我不知道在定义“项目”依赖项时,您可以通过包含特定配置来改进! 所以我想我可以在Alpha中做这样的事情(Beta定义引用的配置): dependencies { compile project('Beta') { configuration = 'RESTclient' } } 我现在正在测试它,如果它正常工作将编辑这个答案。 UPDATE 如果您需要,上述方法可以正常工作。 我需要一些不同的东西。 Project Beta提供了Project ...
  • 目前,地理编码请求中不支持api密钥,而且每个api文档似乎都是有意的。 当前参数是来自V2的遗留物,并且在api控制台中没有可用于地理编码api的服务。 https://developers.google.com/maps/documentation/geocoding/?hl=en#Limits 因此,坚持无密钥请求,如果您想监控您的使用情况,您应该记录自己并观察OVER_QUERY_LIMIT响应。 At this time, there's no support for api key in geo ...
  • 你的问题有点模糊,但我会尽力帮你。 如果我遇到这种情况,我会使用api(公共)和秘密(私人)密钥为每个客户端签名。 每个请求都必须包含公钥(作为字段,或标题,或者你喜欢),以便识别客户端,签名标题,以及从a生成的签名(这可能有点过时) ,自从我这样做了一段时间后)HMAC字符串基于发送信息的base64编码和特定客户端的密钥。 由于只有您的服务和客户端应具有此密钥,因此您将能够验证签名是否与客户端不匹配。 包括客户端在其被泄露的情况下重置密钥的方式也是一个好主意。 此时您已经识别并验证了客户端,因此您可以简 ...
  • 据我所知,在WCF中无法做到这一点。 我所做的是使用“简单”的http文件下载。 这几乎不会消耗客户端的内存。 As far as I know there is no way of doing it in WCF. What I did was using a "simple" http file download. This consumes almost no memory by the client.
  • 我建议你使用控制门户使用的方法来获取使用数据。 它使用此方法: http : //sldn.softlayer.com/reference/services/SoftLayer_Metric_Tracking_Object/getSummaryData 使用获取的数据,门户创建图像。 因此,尝试该方法应该包含更准确的数据。 RESTful example: POST https://api.softlayer.com/rest/v3.1/SoftLayer_Metric_Tracking_Object/$ ...
  • 您可以通过自己创建和绑定并连接套接字然后调用ldap_init_fd()将其转换为LDAP连接来实现。 You can do it by creating and binding and connecting the socket yourself and then calling ldap_init_fd() to turn it into an LDAP connection.
  • 要管理客户机密码(您可能已经知道),请在此处提及的代码中包含client_secrets.json 。 在您的方案中,建议客户要求获得自己的帐户并生成自己的客户机密。 正如您在此处所见,在第1部分:帐户和注册中 ,它说: 湾 实体级别接受。 如果您代表某个实体使用API,则您声明并保证您有权将该实体与条款绑定,并且接受条款,您代表该实体这样做(以及所有对“您”的引用)在条款中指的是该实体)。 因此,在这种情况下,您的客户接受使用Google API的条款和条件也很重要,因为Google API会对任何滥用A ...
  • 如果这是一个Web应用程序,默认情况下它是多线程的。 如果不是 - 你仍然可以将它部署在一个servlet容器上,这将是有益的。 线程池由底层容器(例如tomcat)提供。 每个请求都由一个单独的线程提供服务。 唯一要关心的事情是: 不要使用synchronized 清理你使用的任何ThreadLocal变量 If this is a web application, it is multi-threaded by default. If it's not - you still can deploy it ...
  • 您的客户可能会传入ID和密钥。 将这些视为用户名和密码。 这大大降低了某人在API上抛出一堆字符的机会。 应该在数据库中对ID和密钥列进行索引以避免性能问题。 对于像这样的简单字符串键,如果键不匹配,数据库不会给我任何结果,这是我个人的偏好。 这样,我不必担心发送给客户端的安全漏洞。 所以,是的,密钥应该在where子句中,但它不应该是where子句中唯一的东西。 Your client should likely be passing in an ID and the secret key. Think ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。