“使用 get 时出错"在 Matlab GUI 中使用“addlistener"函数 [英] "Error using get" using a 'addlistener' function in Matlab GUI

查看:29
本文介绍了“使用 get 时出错"在 Matlab GUI 中使用“addlistener"函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Matlab GUI 代码中遇到问题.例如,我想在控制台中显示 GUI 中滑块光标的值.但事实是我想实时显示它,例如在光标的每个位置,即使点击仍然打开,同时移动它.

I have a problem in a Matlab GUI code. Let say for instance that I want to display in the console the value of a slider cursor in the GUI. But the fact is that I want to display it in real time, eg at each position of the cursor even if the click is still on, while moving it.

为此,我在互联网上读到addlistener"功能可以帮助我.我把它放在slider_CreateFcn 函数中是这样的:

For this, I read on internet that the 'addlistener' function could help me. I put it in the slider_CreateFcn function like this :

function slider1_CreateFcn(hObject, eventdata, handles)
   h=addlistener(hObject,'Value','PostSet',@(~,~)slider1_Callback)

然后,我在回调函数中添加了一个简单的 disp 函数,如下所示:

Then, I added a simple disp function in the callback function, like this :

function slider1_Callback(hObject, eventdata, handles)
    get(hObject,'value')

运行此代码会引发此错误:

Running this code raise this error :

Warning: Error occurred while executing callback:
Error using get
Cannot find 'get' method for matlab.graphics.internal.GraphicsMetaProperty class.

Error in untitled>slider1_Callback (line xx)
get(hObject,'value')

如果我删除了addlistener 函数,显然更新不再是实时的,但是我没有收到错误信息.所以我认为问题直接来自 addlistener 函数.

If I remove the addlistener function, obviously the update is no more in real time, but I don't get the error message. So I think that the problem is directly coming from the addlistener function.

发生了什么,我该如何解决?

What is happening and how can I fix it?

推荐答案

首先,您发布的代码并不是产生错误的代码.我猜测产生错误的代码如下所示:

First of all, the code that you posted isn't the code that is producing your error. I'm guessing that the code that yielded your error looked like this:

h = addlistener(hObject, 'Value', 'PostSet', @slider1_Callback);

在这种情况下,元属性作为第一个输入参数传递给 slider1_Callback,这会给您提供您所看到的即时错误.

In this instance, a meta property is passed as the first input argument to slider1_Callback which is giving you the immediate error you're seeing.

话虽如此,如果您想调用 slider1_Callback,您需要制作一个匿名函数,该函数实际上将正确的输入类型(和数量)传递给回调函数.这是一个这样做的.

That being said, if you want to call slider1_Callback you need to craft an anonymous function which actually passes the correct type (and number) of inputs to the callback function. Here is one which does that.

function slider1_CreateFcn(hObject, eventdata, handles)
    h = addlistener(hObject, 'Value', 'PostSet', ...
                    @(src,evnt)slider1_Callback(hObject, [], handles))
end

不过,更好的做法是使用单独的回调,而不是 GUIDE 为您创建的回调.这为您提供了更多的灵活性.此外,如果您只想显示值,则不需要所有其他输入,您实际上可以内联整个回调而不是单独的函数.

The better thing to do though, is to just use a separate callback rather then the one that GUIDE creates for you. This gives you a little bit more flexibility. Also, if you just want to display the value you don't need all of the other inputs and you can actually inline the entire callback rather than having a separate function.

h = addlistener(hObject, 'Value', 'PostSet', @(s,e)disp(get(hObject, 'Value')));

并在行动中展示它:

这篇关于“使用 get 时出错"在 Matlab GUI 中使用“addlistener"函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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