“使用get的错误”在Matlab GUI中使用'addlistener'函数 [英] "Error using get" using a 'addlistener' function in Matlab GUI
问题描述
我在Matlab GUI代码中遇到问题。比方说,我想要在控制台中显示GUI中滑块光标的值。但事实是,我想要实时显示它,例如在光标的每个位置上,即使点击仍在打开状态,同时移动它。
为此,我在互联网上看到'addlistener'功能可以帮助我。我把它放在这样的slider_CreateFcn函数中:
$ p $ function slider1_CreateFcn(hObject,eventdata,handles)
h = addlistener( hObject,'Value','PostSet',@(〜,〜)slider1_Callback)
然后,我
函数slider1_Callback(hObject,eventdata,handles)
在回调函数中添加了一个简单的disp函数,如下所示: get(hObject,'value')
运行此代码会引发此错误:
警告:执行回调时发生错误:
使用get
的错误无法找到matlab.graphics.internal的'get'方法。 GraphicsMetaProperty类。
无题中的错误> slider1_Callback(line xx)
get(hObject,'value')
如果我删除了 addlistener
函数,显然更新不再是实时的,但我没有收到错误消息。所以我认为问题直接来自 addlistener
函数。
发生了什么事情,我该如何解决它? 首先,您发布的代码不是导致错误的代码。我猜测得到你的错误的代码看起来像这样:
$ b $ pre $ h $ addlistener(hObject,'Value',' PostSet',@ slider1_Callback);
在此实例中,元属性作为第一个输入参数传递给 slider1_Callback
这就给你提供了即时的错误。
就是说,如果你想调用 slider1_Callback
你需要创建一个匿名函数,它实际上将正确的类型(和数量)的输入传递给回调函数。
函数slider1_CreateFcn(hObject,eventdata,handles)
h = addlistener(hObject,' Value','PostSet',...
@(src,evnt)slider1_Callback(hObject,[],handles))
end
更好的做法是仅使用单独的回调函数,而不是GUIDE为您创建的回调函数。这给你更多的灵活性。此外,如果您只想显示该值,则不需要所有其他输入,并且可以实际内联整个回调,而不是具有单独的函数。
h = addlistener(hObject,'Value','PostSet',@(s,e)disp(get(hObject,'Value')));
并将其展示出来:
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.
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)
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')
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);
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.
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
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')));
And to show it in action:
这篇关于“使用get的错误”在Matlab GUI中使用'addlistener'函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!